Skip Menu |
 

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Exception-Class-TryCatch CPAN distribution.

Report information
The Basics
Id: 14025
Status: resolved
Priority: 0/
Queue: Exception-Class-TryCatch

People
Owner: dagolden [...] cpan.org
Requestors: cpan.org [...] philipdouglass.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in:
  • 1.07
  • 1.08
Fixed in: (no value)



Subject: try {} and do {cleanup()} idiom doesn't work?
Download (untitled) / with headers
text/plain 1.2k
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";
Good catch. (No pun intended.) An earlier version of try always returned 1 and I was trying to preserve the "try eval {} and" semantic. Of course -- if the eval fails, try gets undef and passes that along. I'll have to look into having try in a void return context always return 1 so that compound idioms work. (Unless that starts making it all too context sensitive and confusing, in which case I'll have to clean up the example.) You really don't need the "try eval {} and" at all -- I was trying to help make things readable, but it's really a null operation. You can just as easily do: try eval { file_error "got a file error"; }; say "Cleanup!"; catch my $err; say $err ? $err->message : "No error"; try really doesn't do anything except store away $@. It's there so that you can do this kind of thing: try eval { file_error "got a file error"; }; try eval { finally(); }; if ( caught my $finally_err ) { # gets the second try die "finally() was fatal"; ) if ( caught my $err ) { # gets the first try say $err ? $err->message : "No error"; }


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.