Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do I redirect STDOUT to a string?

by pm5 (Acolyte)
on Jun 23, 2004 at 04:39 UTC ( [id://368941]=perlquestion: print w/replies, xml ) Need Help??

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

Since this works:
use IO::Handle; use IO::File; my $out = IO::File->new('output.txt', 'w') or die $!; STDOUT->fdopen($out, 'w') or die $!; print "hello, world!";
I thought this should work:
use IO::Handle; use IO::Scalar; my $data = ''; my $out = IO::Scalar->new(\$data) or die $!; STDOUT->fdopen($out, 'w') or die $!; print "hello, world!";
Alas it does not. I'm trying to save the printed output of 3rd party code in a string for further use. How do I do this?

Replies are listed 'Best First'.
Re: How do I redirect STDOUT to a string?
by jZed (Prior) on Jun 23, 2004 at 05:02 UTC
    use IO::Scalar; $std_out = tie *STDOUT, 'IO::Scalar'; print "hello world"; # goes into $std_out scalar;
Re: How do I redirect STDOUT to a string?
by BrowserUk (Patriarch) on Jun 23, 2004 at 06:35 UTC

    If your using 5.8.x, then you can use the "in-memory file" form of open.

    #! perl -slw use strict; sub IO2Mem{ my( $memRef, $mode, $io ) = @_; die 'Usage: IO2Mem( scalarRef [, mode] )' unless ref $memRef eq 'SCALAR'; open $io, $mode || '>', $memRef or die $!; return select $io; } my $memory; my $saved = IO2Mem( \$memory ); print 'Hello world!' for 1 .. 10; $memory =~ s[o w][o, W]g; select $saved; print $memory; __END__ P:\test>test2 Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: How do I redirect STDOUT to a string?
by leriksen (Curate) on Jun 23, 2004 at 05:00 UTC
    You could try Filter::Handle - thats what I do. Be careful though - it was broken in one part by a change in the underlying Perl IO in 5.8+, so some of the test cases for its 'make test' fail. But if you dont need those bits, its great.

    +++++++++++++++++
    #!/usr/bin/perl
    use warnings;use strict;use brain;

Re: How do I redirect STDOUT to a string?
by slife (Scribe) on Jun 23, 2004 at 07:45 UTC

    The "old school" way of doing this was by juggling the current default filehandle with select(), e.g.:

    my $oldfh = select STDOUT; my $out = IO::File->new('output.txt', 'w',) or die $!; select $out; # Do something involving print(). It will go to output.txt. # Be nice and give back STDOUT select $oldfh;

    This functionality is nicely wrapped in the oft-forgotten SelectSaver module (part of the Perl core distribution).

    NB - don't confuse this version of select() with the four-argument version, which reports whether a file descriptor is ready for read/write.

Re: How do I redirect STDOUT to a string?
by DrHyde (Prior) on Jun 23, 2004 at 08:47 UTC
    IO::Capture::Stdout
Re: How do I redirect STDOUT to a string?
by Anonymous Monk on Jun 23, 2004 at 22:42 UTC
    You could use backticks, too.
    my $stdout=`ls`;
    or, perl will fill an array of stdou't lines:
    my @stdout=`ls -al`;
    -vlad

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-07-23 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.