Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello all,

BazB asked me to post a complete example of getting IPC::Open3 to work with IO::Select. Please comment on the code if you see any problems with it.

Thanks a million.

Aziz,,,

############################################################ #!/usr/bin/perl -w # this script printf to stdout and stderr. It prints random # characters and does not flush the output of stdout. stderr # is autoflushed by default. # uncomment the line about autoflush STDOUT to see how that # changes the behavior. Also, you can uncomment the sleep # line to watch the script in slow motion. use warnings; use strict; use IO::Handle; #autoflush STDOUT 1; for (1..100){ my $str = ''; for(1..80){ $str .= ('A'..'Z','a'..'z',0..9)[rand 62]; } if(int rand 2){ # 50:50 chance print STDOUT "StdOut:$str"; } else { print STDERR "StdErr:$str"; } # sleep 1; } ############################################################ #!/usr/bin/perl -w # this script runs the previous script and catches both # child's stdout and stderr. It prints progress information # as it goes. It also prints the data it gets to the # corresponding file. use strict; use warnings; #use diagnostics; use IPC::Open3; use IO::Select; use Symbol; my $cmd = "./test.pl"; open(ERRLOG, ">error.log") or die "Can't open error log! $!"; open(OUTPUT, ">output.log") or die "Can't open output log! $!"; my ($infh,$outfh,$errfh); $errfh = gensym(); # if you uncomment this line, $errfh will # never be initialized for you and you # will get a warning in the next print # line. my $pid; eval{ $pid = open3($infh, $outfh, $errfh, $cmd); }; die $@ if $@; print "IN: $infh OUT: $outfh ERR: $errfh\n"; print "PID was $pid\n"; # now our child is running, happily printing to # its stdout and stderr (our $outfh and $errfh). my $sel = new IO::Select; # create a select object $sel->add($outfh,$errfh); # and add the fhs # $sel->can_read will block until there is data available # on one or more fhs while(my @ready = $sel->can_read) { # now we have a list of all fhs that we can read from foreach my $fh (@ready) { # loop through them my $line; # read up to 4096 bytes from this fh. # if there is less than 4096 bytes, we'll only get # those available bytes and won't block. If there # is more than 4096 bytes, we'll only read 4096 and # wait for the next iteration through the loop to # read the rest. my $len = sysread $fh, $line, 4096; if(not defined $len){ # There was an error reading die "Error from child: $!\n"; } elsif ($len == 0){ # Finished reading from this FH because we read # 0 bytes. Remove this handle from $sel. # we will exit the loop once we remove all file # handles ($outfh and $errfh). $sel->remove($fh); next; } else { # we read data alright print "Read $len bytes from $fh\n"; if($fh == $outfh) { print OUTPUT $line; } elsif($fh == $errfh) { print ERRLOG $line; } else { die "Shouldn't be here\n"; } } } } # now that the child closed both its handles, I assume it # exited. # ps will show you the <defunct> child. print `ps`; # go ahead and reap it waitpid $pid, 0; # wait for it to die # not it's no more print `ps`; close(ERRLOG) or die "Can't close filehandle! $!"; close(OUTPUT) or die "Can't close filehandle! $!";

In reply to Re: IPC::Open3 woes by abstracts
in thread IPC::Open3 woes by BazB

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2014-08-31 09:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (294 votes), past polls