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.)
In reply to Re: Redirecting STDOUT to a variable
by blssu
in thread Redirecting STDOUT to a variable
by Dave05
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |