in reply to Redirect STDOUT to a $variable

See open, where your case is discussed:

close STDOUT; open STDOUT, '>', \$variable or die "Can't open STDOUT: $!";

Replies are listed 'Best First'.
Re^2: Redirect STDOUT to a $variable
by Anonymous Monk on Feb 28, 2009 at 19:59 UTC

    Rather than specifically closing STDOUT I opened a new file handle and selected it

    use strict; print "1: to STDOUT.\n"; my $output = ''; open TOOUTPUT, '>', \$output or die "Can't open TOOUTPUT: $!"; print "2: to STDOUT.\n"; print TOOUTPUT "3: to TOOUTPUT.\n"; select TOOUTPUT; print "4: To STDOUT, (really TOOUTPUT though).\n"; select STDOUT; print "5: To STDOUT again\n"; print TOOUTPUT "6: To TOOUTPUT.\n"; print "----\n".$output."-----\n"; print "Now we're done.\n";
    This is the results
    1: to STDOUT. 2: to STDOUT. 5: To STDOUT again ---- 3: to TOOUTPUT. 4: To STDOUT, (really TOOUTPUT though). 6: To TOOUTPUT. ----- Now we're done.
      Thank you very much, you helped a lot!
Re^2: Redirect STDOUT to a $variable
by iceman_san (Initiate) on Aug 10, 2007 at 13:19 UTC
    open (STDERR,'>&$var'); print $var; hope this wiil help u regards, san FREE as in FREEDOM thats life
      >& does something completely different.