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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use Syntax::Keyword::Try;
# try success
{
my $s;
try {
$s = 1;
}
finally {
$s = 2;
}
is( $s, 2, 'sucessful try{} runs finally{}' );
}
# try failure
{
my $s;
my $e;
ok( !eval {
try {
die "oopsie";
}
finally {
$e = $@;
$s = 3;
}
}, 'failed try{} throws' );
my $dollarat = $@;
is( $s, 3, 'failed try{} runs finally{}' );
like( $e, qr/^oopsie at /, 'finally{} sees $@' );
like( $dollarat, qr/^oopsie at /, 'try/finally leaves $@ intact' );
}
# finally runs on 'return'
{
my $final;
( sub {
try {
return;
}
finally {
$final++;
}
} )->();
ok( $final, 'finally{} runs after return' );
}
# finally runs on 'goto'
{
my $final;
try {
goto after;
}
finally {
$final++;
}
after:
ok( $final, 'finally{} runs after goto' );
}
# finally runs on 'last'
{
my $final;
LOOP: {
try {
last LOOP;
}
finally {
$final++;
}
}
ok( $final, 'finally{} runs after last' );
}
done_testing;
|