Check out IO::Scalar. It can be used like this:
Update: If you need to, you can even alias STDOUT to your IO::Scalar handle:perl -MIO::Scalar -le 'my $s; $SH = IO::Scalar->new(\$s); print $SH "f +oo"; print $s'
That technique should enable you to avoid changing the current code.use IO::Scalar; my $s; my $SH = IO::Scalar->new(\$s); { local *STDOUT = $SH; print "foo\n"; } print $s;
Update 2: A better way to do that, rather than aliasing, is to use the tie interface provided by IO::Scalar. I probably should have recalled that, as I answered a similar question just over a month ago.
use IO::Scalar; $string = tie *STDOUT, 'IO::Scalar'; print "foo\n"; print STDERR '$string: ', $string; # Must use stderr as stdout is t +ied.
-sauoq "My two cents aren't worth a dime.";
In reply to Re: Appending print to a variable
by sauoq
in thread Appending print to a variable
by eric256
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |