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!!

In reply to problems redirecting STDOUT and STDERR to a $variable by jeanluca

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.