in reply to Using IO::CaptureOutput in a child process to caputre STDOUT and STDERR

I think that you were missing a few things, primarily a reaper subroutine---without that, it'll always return -1. Here's how I would do it:
!/usr/bin/perl use strict; use warnings; use IO::CaptureOutput qw(capture capture_exec); use Proc::Killall; use POSIX qw(:signal_h :errno_h :sys_wait_h); &runCommand(); my $pid; unless ( $pid = fork ) { &runCommand(); exit; } $SIG{'CHLD'} = \&Reaper; unless ( $pid = fork ) { &runCommand(); exit; } sub runCommand { my ($stdout, $stderr, $success, $exit_code) = capture_exec('perl', '-e', 'print "Test"'); print "stdout: $stdout\n"; print "stderr: $stderr\n"; print "Success: $success\n"; print "exit code: $exit_code\n"; } sub Reaper { my $pid; waitpid(-1, &WNOHANG); } killall('HUP', -$$);
Update: added a kill.
  • Comment on Re: Using IO::CaptureOutput in a child process to caputre STDOUT and STDERR
  • Download Code

Replies are listed 'Best First'.
Re^2: Using IO::CaptureOutput in a child process to caputre STDOUT and STDERR
by Marshall (Canon) on Apr 10, 2011 at 19:16 UTC
    small comment: there is no "&" in front of WNOHANG.
    sub reaper { my $kid; while ( ( $kid = waitpid(-1, WNOHANG) >0 ) ){}; }
      small comment: there is no "&" in front of WNOHANG.

      Because you removed it! :)

      $ perl -Mstrict -Mwarnings -e " print WNOHANG " Name "main::WNOHANG" used only once: possible typo at -e line 1. print() on unopened filehandle WNOHANG at -e line 1.
        WNOHNAG is defined in:

        use POSIX qw(:sys_wait_h); WNOHANG is a constant value. There is no "&" address of this constant value defined.

        Perhaps I should have said no & address of (&) operator is needed before a constant. And that it is wrong to do that.

        Updated with strikes.