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



我可以在Perl5中使用system来运行外部程序。我喜欢把system看作Perl中的一个小型"Linux命令行"。但是,我在Perl6中找不到system的文档。等价物是什么?

Perl6实际上有两个命令来替换Perl5中的system

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

在Perl6中,run试图避免使用shell。它将第一个参数作为命令,其余参数作为该命令的参数,类似于Perl5的system,当它有多个参数时。

例如:

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

另请参阅这篇2014 Perl6Advent关于"运行外部程序"的文章。

除了使用shellrun来替换Perl5中的system之外,您还可以使用NativeCall来调用libcsystem函数。

在我的Windows盒子上,它看起来像这样:

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

最新更新