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

Re: capturing STDOUT

by AgentM (Curate)
on Apr 30, 2001 at 02:20 UTC ( #76517=note: print w/ replies, xml ) Need Help??


in reply to capturing STDOUT

You could redirect STDOUT to a pipe using the pipe creation command. Then, when you're done, you can read the entire pipe into a variable.

my $bologna; { pipe(READ,WRITE); local *STDOUT=*WRITE; local $|=1; #don't forget to unbuffer (your evil STDOUT hard-coded code) $bologna=<READ>; } print $bologna;
Voila. Pipes kick *ss when it comes to storing variable amounts of data and handle redirection. Of course, this isn't a problem in PerlLand, but I welcome you to CLand, if you'd tag along...
AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.


Comment on Re: capturing STDOUT
Download Code
Re: Re: capturing STDOUT
by premchai21 (Curate) on Apr 30, 2001 at 05:17 UTC
    I think you have the backwards; it should be { local *STDOUT=*WRITE; &evil_STDOUT_code(); } Nice Weird Al Yankovic reference, by the way.
Re: Re: capturing STDOUT
by DrZaius (Monk) on Apr 30, 2001 at 08:34 UTC
    I can't seem to get this to work. I have the following code, but on my linux/perl5.6.0 system, strace says it is blocking on the read.
    #!/usr/bin/perl -w use strict; my $stuff; { pipe(READ,WRITE); local *STDOUT=*WRITE; print "Into the pipe\n"; local $/; $stuff = <READ>; } print "Pipe has: $stuff";
    Any suggestions?
      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)];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-10-21 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls