Why doesn't Perl eval pick up problems with Test :: Cmd :: Common-> unlink?

I have the following perl code:

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";

The line $ test-> unlink () will fail and throw an exception. but the problem: eval does not handle this exception and code execution is interrupted.

output:

$ 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.

Is eval working right here? or am I not understanding something?

F.

+5
source share
2 answers

From the documentation of Test :: Cmd :: Common: "Deletes the specified files. Exit NO RESULT if any file cannot be deleted for any reason.". And, looking at the source, Test :: Cmd :: Common calls Test :: Cmd-> no_result, which actually does

exit (2);

"exit" cannot be captured by eval, so behavior is expected.

+11

, , "" , Test::Exception:

use strict;
use warnings;
use Test::More tests => 2;
use Test::Exception;

note 'File system operations';
dies_ok
    { some_operation_which_may_die(); }
    'operation died';

throws_ok
    { some_operation_which_may_die(); }
    /String we expect to see on death/,
    'operation died with expected message';
+1

All Articles