use Capture::Tiny 'capture'; my ($stdout, $stderr, $exit) = capture { print "I am Perl!\n"; system "echo", "Hello,", "World!"; }; die "system failed, \$?=$exit" unless $exit==0; print "# STDOUT:\n$stdout"; print "# STDERR:\n$stderr";