oli*_*erg 2 perl module runtime try-catch
我有这个代码目前在我的perl脚本的一部分(工作正常):
try {
no strict 'refs';
require_module $modules{$action_name};
if ( &{"FFTG::${actiontype}::run"}( $action, $fs, $log ) ) {
$log->res("SUCCESS");
} else {
if ( $action->{mandatory}[0] == 1 ) {
$log->res("FAIL, exiting.");
last ACTION;
} else {
$log->res("FAIL, but not mandatory, continuing..");
}
}
}
catch {
$log->res("MODULE NOT FOUND");
print "no module found for action '$actiontype', unable to process this action\n";
#warn $_;
};
Run Code Online (Sandbox Code Playgroud)
但不幸的是,脚本的输出在输出中间显示了这3个错误(所有行以Exiting子例程开头):
starting transfer ..
no module found for action 'Transfer', unable to process this action
no module found for action 'Transfer', unable to process this action
no module found for action 'Archive', unable to process this action
Exiting subroutine via last at ./bin/fftgv2.pl line 130.
Exiting eval via last at ./bin/fftgv2.pl line 130.
Exiting subroutine via last at ./bin/fftgv2.pl line 130.
ending transfer
Run Code Online (Sandbox Code Playgroud)
即使脚本运行完美,我发现在标准输出上显示这些错误"很脏".
有没有办法摆脱它们?(我不能使用if/then等..因为我不希望脚本以这个require_module事件退出).我只是希望我的脚本在无法找到模块(但确实如此)时显示警报,而不是这些警告
再次感谢
在perldoc perldiag我们发现那些警告是在一个名为"退出"的组中.
如果您no warnings "exiting";在fftgv2.pl中的第130行之前使用,则会使该块的其余部分的警告静音.
...当然,注意警告也不是最坏的主意.