如何使用Perl 6运行外部程序?(例如像Perl 5中的"system")

Chr*_*oms 5 perl perl6 raku

我可以system在Perl 5中使用它来运行外部程序.我喜欢把它想象成systemPerl中的微型"Linux命令行".但是,我system在Perl 6中找不到文档.相同的是什么?

Chr*_*oms 9

Perl6实际上有两个命令可以替换systemPerl 5.

在Perl6中,shell将其参数传递给shell,类似于Perl 5,system当它有一个包含元字符的参数时.

在Perl6中,run尽量避免使用shell.它将其第一个参数作为命令,将剩余的参数作为该命令的参数,类似于system具有多个参数的Perl 5 .

例如:

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
Run Code Online (Sandbox Code Playgroud)

另请参阅2014年关于"运行外部程序"的Perl 6 Advent帖子.


Chr*_*oph 5

除了使用shellrun替换systemPerl 5之外,您还可以使用它NativeCall来调用libc system函数.

在我的Windows框中,它看起来像这样:

use NativeCall;
sub system(Str --> int32) is native("msvcr110.dll") { * };
system("echo 42");
Run Code Online (Sandbox Code Playgroud)