1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
use strict;
use warnings;
use Test::More;
BEGIN { use_ok "TryCatch" or BAIL_OUT("Cannot load TryCatch") };
use FindBin qw/$Bin/;
use lib "$Bin/lib";
sub simple_return {
try {
return "simple_return";
return "i wont get here";
} #bar
die("return didn't unwind");
return "bar";
}
is(simple_return(), "simple_return", "try with explicit return");
sub simple_no_return {
try {
my $val = "simple_return"; # Not a return op
}
return "bar";
}
is(simple_no_return(), "bar", "try without explicity return");
sub use_test {
try {
use TryCatchTest;
return TryCatchTest::foo();
}
}
is(use_test(), 42, "use in try block");
my $ran_catch = 0;
my $warnings = '';
$SIG{__WARN__} = sub { $warnings .= join('', @_) };
try {
foo();
} #end of try
catch ($e) {
$ran_catch = 1;
}
is($ran_catch, 0, "Catch block not run");
is($warnings, '', "No warnings from try in not in sub");
=for comment
=cut
sub foo {
return 1;
}
done_testing;
|