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

Re: Redirecting STDOUT to a variable

by blssu (Pilgrim)
on Sep 10, 2002 at 11:26 UTC ( #196624=note: print w/replies, xml ) Need Help??

in reply to Redirecting STDOUT to a variable

Perl 5.8 has the ability to open strings (scalars) as files, but that feature does not work for capturing output of other programs -- use the backtick method above for that.

If you want to capture the output of a subroutine or module inside your own Perl code, the string file technique works well. I'm including it here because this node is likely to turn up in a search.

use strict; use PerlIO::scalar; my $output; my $file; if (open($file, ">", \$output)) { print $file "This is a test.\n"; close($file); print $output; }

If you want to redirect STDOUT to a scalar, close it first and then open it connected to the string scalar. You will get an error if you try to do anything that requires a "real" file. (Calling system(...) will fail for example.)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196624]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-11-27 06:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found