How can I run external programs using Perl 6? (e.g. like “system” in Perl 5)

ε祈祈猫儿з 提交于 2019-12-01 18:25:29
Christoph

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");

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".

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!