use IO::Scalar; $string = tie *STDOUT, 'IO::Scalar'; print "foo\n"; print STDERR '$string: ', $string; # Must use stderr as stdout is tied.