my $cmd = "dir"; my $output = `$cmd 2>&1`; if ($? != 0) { print "stderr: '$output'\n"; } else { print "stdout: '$output'\n"; }