use strict; my $out; { open local(*STDOUT), '>', \$out; foo(); } print "Std: $out\n"; sub foo{ print "from foo"; }