On Mon, 20 May 2002, F. Xavier Noria wrote:

> On Mon, 20 May 2002 10:15:02 +0100 (BST)
> Matt Sergeant <[EMAIL PROTECTED]> wrote:
>
> : >     my $um = UserManager->new;
> : >     # ...
> : >     try {
> : >         $um->write_user($user);
> : >   $um->dbh->commit;
> : >     } catch Exception::DB with {
> : >         my $e = shift;
> : >         debug "Exception: $e";
> : >         $um->dbh->rollback;
> : >     };
> :
> : No. $um is caught in a closure, which could potentially leak.
>
> Wow, thank you, I have that pattern repeated in the code many times.
>
> That is the way I would write that try/catch in Java, where you need to
> have $um in the scope of the try and the catch blocks, what is the right
> way to write that in Perl/Error.pm?

I gave up on Error.pm's try/catch syntax a long time ago - I think it's
hidden closure system combined with perl bugs is just too broken for
production use. Instead I use good old eval:

my $um = UserManager->new;
...
eval {
  $um->write_user($user);
  $um->dbh->commit;
};
if ($@ && $@->isa('Exception::DB')) {
   debug "Exception: $@";
   $um->dbh->rollback;
}

(note: if you expect all exceptions to be references like this, you had
better have a $SIG{__DIE__} handler installed to bless non-blessed
exceptions before re-throwing them - ask me if you need an example of
that)

-- 
<!-- Matt -->
<:->Get a smart net</:->

Reply via email to