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

前端 未结 2 1903
迷失自我
迷失自我 2021-01-18 19:02

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 cann

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 19:56

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

提交回复
热议问题