in reply to Pipe, fork, exec and red-hot pokers.

I know there's Expect and IPC::Open3, but what I am doing *should* work, I believe.

Any if those other options work - why?! I am guessing that underneath they're probably doing the same thing I am. The pragmatic me says 'try the other options', the pedantic me says 'yes, but *why* doesn't this current method work?'.

--
Ash OS durbatulk, ash OS gimbatul,
Ash OS thrakatulk, agh burzum-ishi krimpatul!
Uzg-Microsoft-ishi amal fauthut burguuli.

  • Comment on Re: Pipe, fork, exec and red-hot pokers.

Replies are listed 'Best First'.
Re: Re: Pipe, fork, exec and red-hot pokers.
by lestrrat (Deacon) on Apr 08, 2002 at 07:12 UTC

    This may be a long shot, but *how* are you reading that output? are you using the diamond operator, or are you reading via select(IO::Select) + read/sysread ? It may not have anything to do with your problem at all, but who knows...

    FWIW, something like the following works for me

    # note: this code will go into infinite loop # and is not a particularly good piece of code... use IO::Handle; use IO::Select; my( $readfh, $writefh ) = ( IO::Handle->new(), IO::Handle->new() ); pipe( $readfh, $writefh ); if( my $pid = fork() ) { $writefh->close(); my $select = IO::Select->new( $readfh ); my $buf; while( 1 ) { if( $select->can_read( 1 ) ) { if( read( $readfh, $buf, 4096, 0 ) ) { print "got '$buf'"; } } } } else { $readfh->close; open( STDOUT, sprintf( '>&%d', $writefh->fileno ) ); STDOUT->autoflush(1); exec( 'find', '/usr/local/lib/perl5' ); }
      First, I changed 'read' to 'sysread'. But if the program you are exec()ing is this:
      #!/usr/bin/perl my $i=0; while (1) { print "stdout " . $i++ . "\n"; sleep 1; }
      Then you will find that no output is captured by read. The perl program must set $|=1, which is exactly what I don't want to have to worry about.

      --
      Ash OS durbatulk, ash OS gimbatul,
      Ash OS thrakatulk, agh burzum-ishi krimpatul!
      Uzg-Microsoft-ishi amal fauthut burguuli.