我发现STDERR如果命令执行失败,反引号内的重定向可能会丢失.我对我所看到的行为感到困惑.
$ perl -e 'use strict; use warnings; my $out=`DNE`; print $out' Can't exec "DNE": No such file or directory at -e line 1. Use of uninitialized value in print at -e line 1. $ perl -e 'use strict; use warnings; my $out=`DNE 2>&1`; print $out' Use of uninitialized value in print at -e line 1. $ perl -e 'use strict; use warnings; my $out=`echo 123; DNE 2>&1`; print $out' 123 sh: DNE: command not found …
是否有可能捕获由函数创建的输出,这也会导致异常?
function functionWhichCreatesOutputThenCausesAnException() {
"hello"
1/0
"world"
}
try {
$result = functionWhichCreatesOutputThenCausesAnException
} catch {
$($error[0])
}
Run Code Online (Sandbox Code Playgroud)
我的功能创建的输出显示在我的终端中.我想捕捉"你好".这可能吗?