IPC::Simple - simple, non-blocking IPC
version 0.02
use IPC::Simple; my $ssh = IPC::Simple->new( cmd => 'ssh', args => [ $host ], eol => "\n", ); if ($ssh->launch) { $ssh->send('ls -lah'); # get directory listing $ssh->send('echo'); # signal our loop that the listing is done while (my $msg = $ssh->recv) { # echo's output will be an empty string if ($msg->error) { # I/O error croak $msg; } elsif ($msg->stderr) { # output to STDERR warn $msg; } elsif ($msg->stdout) { # output to STDOUT say $msg; } } $ssh->send('exit'); # terminate the connection $ssh->join; # wait for the process to terminate }
Provides a simplified interface for managing and kibbitzing with a child process.
Creates a new IPC::Simple process object. The process is not immediately launched; see "launch".
IPC::Simple
The command to launch in a child process.
An array ref of arguments to cmd.
cmd
The end-of-line character to print at the end of each call to "send". Defaults to "\n".
"\n"
Once launched, returns the pid of the child process.
Once a child process exits, this is set to the exit status ($?) of the child process.
$?
Once a child process has terminated, this is set to the exit code of the child process.
Starts the child process. Returns true on success, croaks on failure to launch the process.
Sends the child process a `SIGTERM`. Returns immediately. Use "join" to wait for the process to finish.
Blocks until the child process has exited.
Sends a string of text to the child process. The string will be appended with a single newline.
Waits for and returns the next line of output from the process, which may be from STDOUT, from STDERR, or it could be an error message resulting from an I/O error while communicating with the process (e.g. a SIGPIPE or abnormal termination).
STDOUT
STDERR
SIGPIPE
Each message returned by recv is an object overloaded so that it can be treated as a string as well as providing the following methods:
recv
Schedules a callback for the next line of input to be received, returning immediately.
$proc->async(sub{ my $msg = shift; if ($msg->stdout) { ... } });
This is done with "CONDITION-VARIABLES" in AnyEvent, so the same caveats about races and dead locks apply. It is up to the caller to manage their event loop.
True when the message came from the child process' STDOUT.
True when the message came from the child process' STDERR.
True when the message is a sub-process communication error.
IPC::Simple will emit highly verbose messages to STDERR if the environment variable IPC_SIMPLE_DEBUG is set to a true value.
IPC_SIMPLE_DEBUG
Jeff Ober <sysread@fastmail.fm>
This software is copyright (c) 2020 by Jeff Ober.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install IPC::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IPC::Simple
CPAN shell
perl -MCPAN -e shell install IPC::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.