Subject: | try {} and do {cleanup()} idiom doesn't work? |
Version: Exception-Class-TryCatch-1.07
perl -v:
This is perl, v5.8.7 built for MSWin32-x86-multi-thread
(with 7 registered patches, see perl -V for more detail)
Copyright 1987-2005, Larry Wall
Binary build 813 [148120] provided by ActiveState http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun 6 2005 13:36:37
I'm not sure if I'm just confused by the documentation, or if it's not working as described. Here's the snippet from the documentation:
try eval {
# code
1;
} and do {
# cleanup
};
catch my $err;
This implies that the cleanup will run no matter what happens in the eval block, right? Like a finally block. But it doesn't work that way:
try eval {
file_error "got a file error";
1;
} and do {
say "Cleanup!"
};
catch my $err;
say $err ? $err->message : "No error";
This code prints:
got a file error
The cleanup block obviously isn't running. It does run if the eval block doesn't throw an error, but how can you call that a cleanup block if it only runs if there isn't a problem?
The only way I can seem to get the try..catch..finally idiom is by inserting the cleanup logic in front of the error handler, but this doesn't seem elegant:
try eval {
file_error "got a file error";
};
catch my $err;
say "Cleanup!";
say $err ? $err->message : "No error";