in reply to executing an external program

something like...
use IPC::Open3; use POSIX ":sys_wait_h"; package SysCall3; sub exec{ my $self = shift; my $command = shift; $self = {}; my $pid = open3 my $wh, my $rh, my $eh, $command; $self -> { EXITCODE } = $?; close $wh; $self -> { STDOUT } = <$rh>; close $rh; $self -> { STDERR } = <$eh>; close $eh; waitpid $pid,0; return bless $self; } sub getinfo( my $self = shift; return $self -> { shift() }; } 1;
example:
use SysCall3; my $nasty = SysCall3 -> exec( 'bigNastyProgram' ); for my $err ( $nasty -> getinfo( 'STDERR' ) ) { # process the error output print STDERR $err; } for my $out ( $nasty -> getinfo ('STDOUT' ) ) { # process the stdout print $out; } my $code = $nasty -> getinfo( 'EXITCODE' ); # process return code exit $code; # if wanting to pass control back to the o/s
Can also have the exec method leave the filehandles unread and store them in the object hash and another method be used to read from them. In that case, the pid should also be stored in the hash and the close/waitpid need to be invoked whenever needed to prevent zombies.

-M

Free your mind

Replies are listed 'Best First'.
Re^2: executing an external program
by smith111 (Novice) on Jun 22, 2006 at 17:24 UTC
    Thank you Moron, you have freed my mind.