IPC::Open3::Callback - An extension to IPC::Open3 that will feed out and err to callbacks instead of requiring the caller to handle them.
version 1.01
use IPC::Open3::Callback; my $runner = IPC::Open3::Callback->new( out_callback => sub { my $data = shift; my $pid = shift; print( "$pid STDOUT: $data\n" ); }, err_callback => sub { my $data = shift; my $pid = shift; print( "$pid STDERR: $data\n" ); } ); my $exit_code = $runner->run_command( 'echo Hello World' ); use IPC::Open3::Callback qw(safe_open3); my ($pid, $in, $out, $err) = safe_open3( "echo", "Hello", "world" ); $buffer = ''; my $select = IO::Select->new(); $select->add( $out ); while ( my @ready = $select->can_read( 5 ) ) { foreach my $fh ( @ready ) { my $line; my $bytes_read = sysread( $fh, $line, 1024 ); if ( ! defined( $bytes_read ) && !$!{ECONNRESET} ) { die( "error in running ('echo $echo'): $!" ); } elsif ( ! defined( $bytes_read) || $bytes_read == 0 ) { $select->remove( $fh ); next; } else { if ( $fh == $out ) { $buffer .= $line; } else { die( "impossible... somehow got a filehandle i dont know about!" ); } } } } waitpid( $pid, 0 ); my $exit_code = $? >> 8; print( "$pid exited with $exit_code: $buffer\n" ); # 123 exited with 0: Hello World
This module feeds output and error stream from a command to supplied callbacks. Thus, this class removes the necessity of dealing with IO::Select by hand and also provides a workaround for Windows systems.
Passes the command and arguments on to open3 and returns a list containing:
open3
The process id of the forked process
An IO::Handle to STDIN for the process
An IO::Handle to STDOUT for the process
An IO::Handle to STDERR for the process
As with open3, it is the callers responsibility to waitpid to ensure forked processes do not become zombies.
This method works for both *nix and Windows sytems. On a windows system, it will use sockets per http://www.perlmonks.org/index.pl?node_id=811150.
The constructor creates a new Callback object and optionally sets global callbacks for STDOUT and STDERR streams from commands that will get run by this object (can be overridden per call to run_command).
STDOUT
STDERR
A subroutine to call for each chunk of text written to STDOUT. This subroutine will be called with 2 arguments:
A chunk of text written to the stream
The pid of the forked process
A subroutine to call for each chunk of text written to STDERR. This subroutine will be called with the same 2 arguments as out_callback
out_callback
A boolean value, if true, will buffer output and send to callback one line at a time (waits for '\n'). Otherwise, sends text in the same chunks returned by sysread.
The timeout, in seconds, provided to IO::Select, by default 0 meaning no timeout which will cause the loop to block until output is ready on either STDOUT or STDERR.
IO::Select
Will run the specified command with the supplied arguments by passing them on to safe_open3. Arguments can be embedded in the command string and are thus optional.
If the last argument to this method is a hashref (ref(@_[-1]) eq 'HASH'), then it is treated as an options hash. The supported allowed options are the same as the constructor and will be used in preference to the values set in the constructor for this call.
ref(@_[-1]) eq 'HASH'
Returns the exit code from the command.
Lucas Theisen (lucastheisen@pastdev.com)
Copyright 2013 pastdev.com. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
IPC::Open3 IPC::Open3::Callback::Command IPC::Open3::Callback::CommandRunner https://github.com/lucastheisen/ipc-open3-callback http://stackoverflow.com/q/16675950/516433
To install IPC::Open3::Callback::NullLogger, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IPC::Open3::Callback::NullLogger
CPAN shell
perl -MCPAN -e shell install IPC::Open3::Callback::NullLogger
For more information on module installation, please visit the detailed CPAN module installation guide.