Subject: Re: [1.8.0] perl/Git.pm: moving away from using Error.pm module

From: Jakub Narebski <[email-masked]>
Date: Mon, 21 Feb 2011 13:12:44 +0100

On Mon, 21 Feb 2011, Nick wrote:
> On 21/02/11 07:20, Junio C Hamano wrote:

> > If we are going to change things so that everybody uses a
> > more traditional "eval {}; if ($@) { ... }", it would be a welcome change
> > from my point of view.
> 
> A small aside - note the "Dangers of using $@" described here:
> 
>   http://www.socialtext.net/perl5/exception_handling
> 
> To paraphrase, this:
> 
>   eval { stuff ; 1} or do { handle_exception };
> 
> is marginally safer than:
> 
>   eval { stuff }; if (defined $@) { handle_exception }

Important note: it is "if ($@)", not "if (defined $@)":

  If there was no error, $@ is guaranteed to be a null string.
                                                  ^^^^^^^^^^^

It is empty string, not undef.

> because it is possible that $@ can be modified (say, by a DESTROY method) before
> the if clause sees it.  The former idiom does not stop that, it just means your
> exception handler is executed reliably.
> 
> Normally it is not a problem, but this is still something worth knowing.

Or better use Try::Tiny, which takes care of this and more

  use Try::Tiny;
  try { stuff  } catch { handle_exception };

-- 
Jakub Narebski
Poland