class Proc

Running process (filehandle-based interface)

class Proc {}

Proc is a representation of an invocation of an external process. It provides access to the input, output and error stream as well as the exit code. It is typically created through the run subroutine:

my $proc = run 'echo''Hallo world':out;
my $captured-output = $proc.out.slurp: :close;
say "Output was $captured-output.perl()";       # OUTPUT: «Output was "Hallo world\n"␤» 

Piping several commands is easy too. To achieve the equivalent of the pipe echo "Hello, world" | cat -n in Perl 6, and capture the output from the second command, you can do

my $p1 = run 'echo''Hello, world':out;
my $p2 = run 'cat''-n':in($p1.out), :out;
say $p2.out.get;

You can also feed the :in pipe directly from your program, by setting it to True, which will make the pipe available via .in method on the Proc:

my $p = run "cat""-n":in:out;
$p.in.say: "Hello,\nworld!";
$p.in.close;
say $p.out.slurp: :close;
# OUTPUT: «1  Hello,␤ 
#          2  world!␤» 

In order to capture the standard error :err can be supplied:

my $p = run "ls""-l"".""qqrq":out:err;
my $captured-output = $p.out.slurp: :close;
my $captured-error  = $p.err.slurp: :close;
my $exit-code       = $p.exitcode;

Note: Versions of Rakudo older than 2017.04 do not have .slurp available on IO::Pipe objects; use .slurp-rest instead.

Use Proc::Async for non-blocking operations.

Methods

method new

method new(Proc:U:
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
    --> Proc:D)
sub run(
        *@args ($*@),
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
        :$cwd = $*CWD,
        Hash() :$env = %*ENV
    --> Proc:D)
sub shell(
        $cmd,
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
        :$cwd = $*CWD,
        Hash() :$env = %*ENV
    --> Proc:D)

new creates a new Proc object, whereas run or shell create one and spawn it with the command and arguments provided in @args or $cmd, respectively.

$in, $out and $err are the three standard streams of the to-be-launched program, and default to "-", which means they inherit the stream from the parent process. Setting one (or more) of them to True makes the stream available as an IO::Pipe object of the same name, so for example $proc.out. You can set them to False to discard them. Or you can pass an existing IO::Handle object (for example IO::Pipe) in, in which case this handle is used for the stream.

$bin controls whether the streams are handled as binary (i.e. Blob object) or text (i.e. Str objects). If $bin is False, $enc holds the character encoding to encode strings sent to the input stream and decode binary data from the output and error streams.

With $chomp set to True, newlines are stripped from the output and err streams when reading with lines or get. $nl controls what your idea of a newline is.

If $merge is set to True, the standard output and error stream end up merged in $proc.out.

method sink

method sink(--> Nil)

When sunk, the Proc object will throw X::Proc::Unsuccessful if the process it ran exited unsuccessfully.

method spawn

method spawn(*@args ($*@), :$cwd = $*CWDHash() :$env = %*ENV --> Bool:D)

Runs the Proc object with the given command, argument list, working directory, and environment.

method shell

method shell($cmd:$cwd = $*CWD:$env --> Bool:D)

Runs the Proc object with the given command and environment which are passed through to the shell for parsing and execution. See IO::shell for an explanation of which shells are used by default in the most common operating systems.

method command

method command(Proc:D: --> Array:D)

The command method is an accessor to an array containing the arguments that were passed when the Proc object was executed via spawn or shell or run.

method pid

method pid(Proc:D:)

Returns the $*PID value of the process if available, or Nil.

method exitcode

method exitcode(Proc:D: --> Int:D)

Returns the exit code of the external process, or -1 if it has not exited yet.

method signal

method signal(Proc:D:)

Returns the signal number with which the external process was killed, or 0 or an undefined value otherwise.

Type Graph

Type relations for Proc
perl6-type-graph Proc Proc Any Any Proc->Any Mu Mu Any->Mu

Stand-alone image: vector