On 11/8/06, Jay Savage <[EMAIL PROTECTED]> wrote:
There are a couple of things going on here.

First, the issue of what prints when is driven by buffering. Since you
haven't turned on autofulsh on any of your file handles, the buffer is
flushed whenever it's convenient for the the system. The results may
be more what you expect if you print to STDERR instead of STDOUT.

This also looks to be the culprit in your second example. You print to
the child, but you never do anything to guarantee the buffer is
flushed, so the child doesn't see the "\n" on STDIN until Perl
automatically reaps the filehandle and flushes the buffer when the
parent tries to exit. In this case, turn on autoflush on CHILD and see
if you don't get something more like your expected result.

HTH,

-- jay

On 11/8/06, Tom Phoenix <[EMAIL PROTECTED]> wrote:
On 11/8/06, Jen Spinney <[EMAIL PROTECTED]> wrote:

>     print CHILD "printing to child\n";
>     sleep 5;

Concurrent processes can be counterintuitive at times. Because the
CHILD filehandle is buffered, that line won't necessarily be sent to
the child process right away. In your program, I'd expect it to be
sent only once the filehandle is closed (i.e., when the parent process
exits).

If you use the $| variable, or otherwise flush the output buffer
before sleep, I think you'll get results more to your liking.

Cheers!

--Tom Phoenix
Stonehenge Perl Training


Thanks Jay and Tom!  I added this little bit to my script:

select (CHILD);
$| = 1;
select (STDOUT);

and it worked just as I expected it to.  It's interesting, though,
that I've been working with sockets a bunch through IO::Socket and my
print statements seem to print to the socket stream immediately.  Is a
good practice to set the autoflush to one for socket filehandles as
well?

Thanks again,
Jen

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to