# $foo = capture { print 'hi'; } # $foo will be filled as if someone had done $foo = `perl -e { print 'hi'; }` or something like that # if the sub prints directly to STDOUT, like: print STDOUT 'hi'; then the output will not be captured sub capture(&) { my ($oldsel, $fh, $ret); open($fh, '>', \$ret) or die "can't capture: $!"; $oldsel = select($fh); &{$_[0]}; select $oldsel; close($fh); $ret; }