The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::Run3 - Run a subprocess and read/write to it

SYNOPSIS

my $run3 = Mojo::Run3->new;
$run3->on(read => sub ($run3, $bytes, $conduit) {
  print STDOUT $bytes if $conduit eq 'stdout';
});

$run3->run_p(sub { exec qw(/usr/bin/ls -l /tmp) })->wait;

DESCRIPTION

Mojo::Run3 allows you to fork a subprocess which you can "write" STDIN to, and "read" STDERR and STDOUT, without blocking the the event loop.

This module is currently EXPERIMENTAL, but unlikely to change much.

EVENTS

drain

$run3->on(drain => sub ($run3) { });

Emitted after "write" has written the whole buffer to the subprocess.

error

$run3->on(error => sub ($run3, $str) { });

Emitted when something goes wrong.

finish

$run3->on(finish => sub ($run3) { });

Emitted when the subprocess has ended. "error" might be emitted before "finish", but "finish" will always be emitted at some point after "start" as long as the subprocess actually stops. "status" will contain $! if the subprocess could not be started or the exit code from the subprocess.

read

$run3->on(finish => sub ($run3, $bytes, $conduit) { });

Emitted when the subprocess write bytes. $conduit can be "stdout" or "stderr".

spawn

$run3->on(spawn => sub ($run3) { });

Emitted in the parent process after the subprocess has been forked.

ATTRIBUTES

ioloop

$ioloop = $run3->ioloop;
$run3   = $run3->ioloop(Mojo::IOLoop->singleton);

Holds a Mojo::IOLoop object.

METHODS

close

$run3 = $run3->close('stdin');

Can be used to close STDIN. This is useful after piping data into a process like cat.

exit_status

$int = $run3->exit_status;

Returns the exit status part of "status", which will should be a number from 0 to 255.

kill

$int = $run3->kill($signal);

Used to send a $signal to the subprocess. Returns -1 if no process exists, 0 if the process could not be signalled and 1 if the signal was successfully sent.

pid

$int = $run3->pid;

Process ID of the subprocess after "start" has successfully started.

run_p

$p = $run3->run_p(sub ($run3) { ... })->then(sub ($run3) { ... });

Will "start" the subprocess and the promise will be fulfilled when "finish" is emitted.

start

$run3 = $run3->start(sub ($run3, @) { ... });

Will start the subprocess. The code block passed in will be run in the child process. The code below can be used if you want to run another program:

$run3 = $run3->start(sub { exec @my_other_program_with_args });
$run3 = $run3->start(sub { exec qw(/usr/bin/ls -l /tmp) });

status

$int = $run3->status;

Holds the exit status of the program or $! if the program failed to start. The value includes signals and coredump flags, but "exit_status" can be used be used to get the value from 0 to 255.

write

$run3 = $run3->write($bytes);
$run3 = $run3->write($bytes, sub ($run3) { ... });

Used to write $bytes to the subprocess. The optional callback will be called on the next "drain" event.

AUTHOR

Jan Henning Thorsen

COPYRIGHT AND LICENSE

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

Mojo::IOLoop::ReadWriteFork, IPC::Run3.