fedelman has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys, I'm coding a perl script. I should catch the "exit code" (like $?) of the process. I'm using IPC::Open3 because I need read the "stdout" and "stderr" in real-time when the execution is performed. My trouble is I can't found anyway to catch the "exit code" from "test.pl". How can I do it? Thanks in advance for your help. Fede The script like something like this:
--- snip snip --- #!/usr/local/bin/perl use strict; use warnings; use IO::Handle; use IO::Select; use IPC::Open3; use POSIX ":sys_wait_h"; ++$|; # force flush #+-------------------------------------------------------------------- +---+# # Sub: spawn_proc # Spawn process # Input: (null) # Return: (null) #+-------------------------------------------------------------------- +---+# sub spawn_proc { my $cmd = "./test.pl"; local $SIG{PIPE} = sub { die "pipe broke" }; my $ref_rfh = \*RDRFH; my $ref_efh = \*ERRFH; my $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH, $cmd); my $sel = IO::Select->new(); $ref_rfh->autoflush(1); $ref_efh->autoflush(1); $sel->add($ref_rfh, $ref_efh); print "Executing: $cmd\n"; while( my @ready = $sel->can_read ) { foreach my $fh (@ready) { my $line = $fh->getline; if($fh->fileno == $ref_rfh->fileno) { print "stdout: $line" if($line); } elsif ($fh->fileno == $ref_efh->fileno) { print "stderr: $line" if($line); } $sel->remove($fh) if(eof($fh)); } } close (RDRFH) || die("1 Return: $? | $@ | $!\n"); close (ERRFH) || die("2 Return: $? | $@ | $!\n"); close (WTRFH) || die("3 Return: $? | $@ | $!\n"); print "Done.\n"; } spawn_proc(); exit(0); --- snip snip ---
Edit by castaway - fixed mixed code/pre tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Catching "exit code" from IPC::Open3
by BrowserUk (Patriarch) on Jun 22, 2004 at 22:06 UTC | |
by fedelman (Novice) on Jun 22, 2004 at 22:43 UTC | |
by BrowserUk (Patriarch) on Jun 23, 2004 at 00:38 UTC | |
|
Re: Catching "exit code" from IPC::Open3
by etcshadow (Priest) on Jun 22, 2004 at 21:19 UTC | |
by fedelman (Novice) on Jun 22, 2004 at 22:04 UTC |