I can use system
in Perl 5 to run external programs. I like to think of system
like a miniature "Linux command line" inside Perl. However, I cannot find documentation for system
in Perl 6. What is the equivalent?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In addition to using shell
or run
, which replace system
from Perl 5, you can also use NativeCall
to invoke the libc system
function.
On my Windows box, it looks like this:
use NativeCall;
sub system(Str --> int32) is native("msvcr110.dll") { * };
system("echo 42");
回答2:
Perl6 actually has two commands that replace system
from Perl 5.
In Perl6, shell
passes its argument to the shell, similar to Perl 5's system
when it has one argument that contains metacharacters.
In Perl6, run
tries to avoid using the shell. It takes its first argument as a command and the remaining arguments as arguments to that command, similar to Perl 5's system
when it has multiple arguments.
For example:
shell('ls > file.log.txt'); # Capture output from ls (shell does all the parsing, etc)
run('ls','-l','-r','-t'); # Run ls with -l, -r, and -t flags
run('ls','-lrt'); # Ditto
See also this 2014 Perl 6 Advent post on "running external programs".