use warnings; use strict; use IO::Scalar; my $output; tie (*STDOUT, 'IO::Scalar', \$output); # Use this to capture STDOUT print `echo foo`; untie *STDOUT; # Use this to return STDOUT to normal print "my output='$output'" __DATA__ my output='foo '