Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Redirect STDOUT to a $variable

by jeanluca (Deacon)
on Aug 10, 2007 at 11:26 UTC ( #631760=perlquestion: print w/replies, xml ) Need Help??
jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I would like to capture everyting thats printed into a variable ( so I can wrap this into XML later on).
This is my test script so far
#!/usr/bin/perl -l use strict; use warnings; my $x ; { local *STDOUT ; open STDOUT, ">$x" or die "nope\n"; print "this is stored in \$x" ; } print $x ;
Which doesn't work of course, perl now thinks $x contains a filename.
Any suggestions how to do this ?


UPDATE: Thanks a lot for the solutions/suggestions!!

Replies are listed 'Best First'.
Re: Redirect STDOUT to a $variable
by Corion (Pope) on Aug 10, 2007 at 11:31 UTC

    See open, where your case is discussed:

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

      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!
      open (STDERR,'>&$var'); print $var; hope this wiil help u regards, san FREE as in FREEDOM thats life
        >& does something completely different.
Re: Redirect STDOUT to a $variable
by Moron (Curate) on Aug 10, 2007 at 11:53 UTC
    It's not abundantly clear what is wanted although taking your first statement in isolation, I would deduce (tentatively!) that (on *NIX) you'd want something like: | tee trapit.txt
    and then postprocess what landed in the siphoned-off text file.

    On Windows there is no tee, so you'd have to overload the print function with your own routine to print both to STDOUT and somewhere else.

    Update: BUT if you can live with the output being completely redirected rather than just copied then... > trapit.txt
    works on Windows and *NIX

    ^M Free your mind!

Re: Redirect STDOUT to a $variable
by citromatik (Curate) on Aug 10, 2007 at 11:44 UTC

    Maybe it is enough to use sprintf:

    use strict; use warnings; my $x; $x = sprintf ("%s","this is stored in \$x"); $x .= sprintf ("%s","... and this too\n"); print $x;


    Update:Corrected sprintf syntax

Re: Redirect STDOUT to a $variable
by bruceb3 (Pilgrim) on Aug 10, 2007 at 21:16 UTC
    Maybe the IO::String module from CPAN would give you the functionality that you are seeking?
Re: Redirect STDOUT to a $variable
by Anonymous Monk on Nov 13, 2007 at 17:33 UTC
    I found that this works: sub print_to_var { my ($m, $v) = @_; local *STDOUT; open(STDOUT, ">", \$v); print $m; return $v; } #returns variable which contains string of $m printed

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://631760]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-02-22 01:17 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.