Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)];

In reply to Re: Re: Re: capturing STDOUT by Rhandom
in thread capturing STDOUT by nop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found