in reply to Open, ye! And read from your child!

Along with having buffering on, your <READ> is in list context, sucking up all input, which doesn't appear to be what you want. Try this:
$|++; if (my $pid = open(READ, "-|")) { my $line = <READ>; print "child said: [", $line, "]; ",time,"\n"; $line = <READ>; print "child said: [", $line, "]; ",time,"\n"; } else { print "hello; ",time,"\n"; sleep 2; print "how are you; ",time,"\n"; }

Replies are listed 'Best First'.
Re: Re: Open, ye! And read from your child!
by gryng (Hermit) on Nov 13, 2001 at 01:42 UTC

    Doh! You would think a (relatively) seasoned Perl Junkie would have noticed that list context blurp. Ok the new code that works is:

    #!/usr/bin/perl -w use strict; if (my $pid = open(READ, "-|")) { print "child said: '", scalar <READ>, "'; ",time,"\n"; print "child said: '", scalar <READ>, "'; ",time,"\n"; } else { $|=1; print "hello; ",time,"\n"; sleep 2; print "how are you; ",time,"\n"; }
    And output:
    gryn@echor:~$ ./testcase.pl child said: 'hello; 1005597768 '; 1005597768 child said: 'how are you; 1005597770 '; 1005597770

    The buffering is necessary, as you all pointed out. However, with the list context problem in the way, I couldn't figure out where to properly put the (de)buffering until it was fixed.

    Thanks and kudos to you three,
    Gryn