use IO::Scalar; my $s; my $SH = IO::Scalar->new(\$s); { local *STDOUT = $SH; print "foo\n"; } print $s;