http://www.perlmonks.org?node_id=720352

diabelek has asked for the wisdom of the Perl Monks concerning the following question:

I can't seem to get an exec call to print to a redefined STDOUT that is actually a pipe. Here's the code where I create the pipe and the child process


# dup stdout so we can restore it later
   if( !open( $old_stdout, ">&STDOUT" ) ) {
      return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
   }
   close( STDOUT );

   # create the piped stdout
   pipe( $smash_stdout, STDOUT );

   # set non blocking and unbuffered
   my $flags = fcntl( STDOUT, F_GETFL, 0 );
   fcntl( STDOUT, F_SETFL, $flags | O_NONBLOCK );
   select((select(STDOUT), $|=1)[0]);

   if( ($pid = fork()) ) {
      # parent process
      # reset out STDOUT
      if( !open( STDOUT, ">&", $old_stdout ) ) {
         return common::results->new( FAILURE, "Unable to dup STDOUT: $!" );
      }
      close( $old_stdout );
      $target->{stdout} = $smash_stdout;
      $target->{buffer} = "";
   } else {
      exec ($cmd);
      exit( -1 );
   }

And then later on in the parent thread, I run the following


      foreach $target (@{$self->{TARGETS}}) {
         if( defined( $target->{stdout} ) ) {
            my $smash_fh = $target->{stdout};
            $target->{buffer} .= <$smash_fh>;
         }
      }

If I print something to STDOUT before I exec, I read that through the pipe but the exec routine doesn't seem to send it through the pipe. I've deleted the code to redo stdout and the exec will print to the screen so the exec'ed program is printing, just not to my new STDOUT. Is there something different that I need to do?