Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

problems redirecting STDOUT and STDERR to a $variable

by jeanluca (Deacon)
on Feb 15, 2008 at 13:18 UTC ( [id://668132]=perlquestion: print w/replies, xml ) Need Help??

jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I'm trying to redirect both STDOUT and STDERR to the same variable. Here is the test code:
use strict; use warnings; my $out ; { local *STDOUT ; local *STDERR ; open STDOUT, '>>', \$out or die "Can't open STDOUT: $!"; # line 28 open STDERR, '>>', \$out or die "Can't open STDERR $!"; # line 29 select STDOUT; $| = 1; # make unbuffered select STDERR; $| = 1; # make unbuffered print "message1\n" ; warn "error1\n" ; print "message2\n" ; } print "Result: $out \n" ;
Output:
Use of uninitialized value in open at ./t2.pl line 29. Result: message1 error1 message2
So, line 29 has a problem while line 28 has not, any suggestions why?

Next I tried to run it as a cgi program (with apache):
use CGI qw(:standard append add_parameter); use strict ; use warnings ; print CGI::header() ; my $out ; { local *STDOUT ; local *STDERR ; open (STDOUT,">>", \$out) ; open (STDERR,">>", \$out) ; select STDOUT; $| = 1; # make unbuffered select STDERR; $| = 1; # make unbuffered print "message1\n" ; warn "error1\n"; print "message2\n" ; } print "Result: $out\n";
In the browser I get nothing(blank), but running it from the commandline I get what I expected:
$ ./t2.pl Content-Type: text/html; charset=ISO-8859-1 Use of uninitialized value in open at ./t2.pl line 29. Result: message1 error1 message2
But if I remove the line select STDERR; $| = 1; everything works fine in the browser too!
Any reason why ? or should I do this differently ?

Thnx a lot
LuCa

ps I'm using perl v5.8.8 on Linux

Update Thnx monks, thats what I needed!!

Replies are listed 'Best First'.
Re: problems redirecting STDOUT and STDERR to a $variable
by kyle (Abbot) on Feb 15, 2008 at 13:31 UTC
Re: problems redirecting STDOUT and STDERR to a $variable
by philcrow (Priest) on Feb 15, 2008 at 16:02 UTC
    When you print without specifying a handle, output goes to the selected handle, which defaults to STDOUT. If you select STDERR, print goes there by default instead. For CGI to work, you must print what the browser receives on STDOUT.

    One solution would be to reverse the order of the selects, so STDOUT is left selected after the hot buffering request. Normal practice is to store the return value of the first select and restore it after you finish messing with $|:

    my $original_fh = select STDOUT; $| = 1; select STDERR; $| = 1; select $original_fh;
    This works even if someone had already hijacked the default output handle.

    Phil

    The Gantry Web Framework Book is now available.
      I prefer
      use IO::Handle qw( ); STDOUT->autoflush(1); STDERR->autoflush(1);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://668132]
Approved by olus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-24 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found