Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Re: capturing STDOUT

by Rhandom (Curate)
on Apr 30, 2001 at 18:12 UTC ( #76589=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: capturing STDOUT
in thread capturing STDOUT

Just a guess... But I think that it is still blocking on the READ handle because the WRITE handle is still open.

Yup, just checked. That is what is happening...

#!/usr/bin/perl -w pipe(READ,WRITE); print WRITE "Good\n"; close(WRITE); print <READ>;

Try that code with and without the close(WRITE) line commented out. If you comment it out it hangs.

Here is some more trouble that you're going to run into. Because you need to close the WRITE handle before the <READ> will stop, you are going to run into a buffer limitation (actually this could happen anyway). On many systems a pipe buffer can only accept 4k to 8k before it blocks. It will block until somebody cleans it out by reading the READ end. This means if the output of your command is too big, just executing it will block as it tries to put all of it's info into the WRITE handle.

The way to fix all of this is to pipe, fork, have the parent READ and the child WRITE.

#!/usr/bin/perl -w pipe(READ,WRITE); my $pid = fork; die $! unless defined $pid; ### parent if( $pid ){ local $/ = 1; # never do this global my $out = <READ>; ### child }else{ # local *STDOUT = *WRITE; print WRITE "Good\n"; close(WRITE); exit; } print "I got [$out]\n";
my @a=qw(random brilliant braindead); print $a[rand(@a)];


Comment on Re: Re: Re: capturing STDOUT
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://76589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-08-21 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (133 votes), past polls