加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么Perl的eval没有从Test :: Cmd :: Common-> unlink中捕

发布时间:2020-12-15 21:46:55 所属栏目:大数据 来源:网络整理
导读:我有以下perl代码: use strict;use warnings;use Test::Cmd::Common;my $path = "/something/not/available";my $test = Test::Cmd::Common-new(string = 'File system operations');eval{ $test-unlink("$path");};ok(!$@,"file unlike");print "done.n";
我有以下perl代码:
use strict;
use warnings;
use Test::Cmd::Common;

my $path = "/something/not/available";
my $test = Test::Cmd::Common->new(string => 'File system operations');

eval{
        $test->unlink("$path");
};
ok(!$@,"file unlike");

print "done.n";

$test-> unlink()行将失败并抛出异常.但问题是:eval没有处理该异常并且代码执行正在被中断.

输出 :

$perl test.pl 
could not unlink files (/something/not/available): No such file or directory
NO RESULT for test at line 561 of /home/y/lib/perl5/site_perl/5.8/Test/Cmd/Common.pm (Test::Cmd::Common::unlink)
    from line 9 of test.pl.

eval在这里做得对吗?或者我误解了什么?

F.

解决方法

从Test :: Cmd :: Common的文档:“删除指定的文件.如果由于任何原因无法删除任何文件,则退出无结果.”.通过查看源代码,Test :: Cmd :: Common调用Test :: Cmd-> no_result,这确实是
exit (2);

“退出”不能被eval困住,所以它是预期的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读