Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
You could go for IPC::Open3, but I've had mixed results with it (because I'm too clever for my own good). I usually do a full blown pipe, fork and exec -- maybe something like:
pipe my $cin, my $pin; pipe my $pout, my $cout; pipe my $perr, my $cerr; $pin->autoflush(1); $cout->autoflush(1); $cerr->autoflush(1); my $pid = fork(); warn("Fork failed\n"),return unless defined $pid; if ($pid == 0) { # Child fork/TDE execute close $pin; open STDIN, "<&", $cin or die "Map fail (STDIN): $!, $?\n"; exit 1 if fileno(STDIN) != 0; close $pout; close STDOUT; open STDOUT, ">&", $cout or die "Map fail (STDOUT): $!, $?\n"; exit 1 if fileno(STDOUT) != 1; close $perr; close STDERR; open STDERR, ">&", $cerr or print("Map fail (STDERR): $!, $?\n +"),exit 1; exit 1 if fileno(STDERR) != 2; exec $command; } # Parent fork/tester return 0 if eval { alarm $timeout; local $SIG{ALRM} = sub { kill 9, $pid; die "Calculation call failed to return with $timeout secon +ds\n"; }; close $cin; close $cout; close $cerr; close $pin; local $/; # Slurp my $content = eof $pout ? '' : <$pout>; my $error = eof $perr ? '' : <$perr>; alarm 0; unless ($content =~ /\S/) { if ($error =~ /\S/) { $error =~ s/^/\t/mg; $error =~ s/\s*$/\n/; die "No result returned; STDERR=\n$error"; } else { die "Case Aborted: No result, no error\n"; } } elsif ($error =~ /\S/) { $error =~ s/^/\t/mg; warn "$executable output to STDERR for $case$command\n$err +or"; } }
where obviously, you'll nee to rework the error handling a little. This will give you complete control over all 3 pipes.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: How to capture child process output and divert to log4perl by kennethk
in thread How to capture child process output and divert to log4perl by chrestomanci

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 pondering the Monastery: (3)
    As of 2014-09-20 14:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (159 votes), past polls