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


In reply to Catching "exit code" from IPC::Open3 by fedelman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.