develooper Front page | perl.perl5.porters | Postings from June 2008

Re: Fatal/autodie exception hierarchies for Perl 5

Thread Previous | Thread Next
From:
Dr.Ruud
Date:
June 9, 2008 16:03
Subject:
Re: Fatal/autodie exception hierarchies for Perl 5
Message ID:
20080609230334.23309.qmail@lists.develooper.com
"Joshua ben Jore" schreef:
> Dr.Ruud:
>> Joshua ben Jore:

>>> Semi-recently at work I found that a few buggy exception objects in
>>> the global $@ were clearing themselves out of $@ as a side effect of
>>> examining them for truth.
>>
>> Oops, I'd better change my
>>
>>    push @errors, $@ if $@;
>>
>> to something like
>>
>>    {my $e; push @errors, $e if $e = $@}
>
> No, that's only if you have exception objects which might also have
> overloading.

This was about any kind, so including those.


> Also, you can only trust $@ as long as you also trust
> that you'll never have objects around that can clear $@ during their
> DESTROY phase. The presence of objects which can set $@ during their
> DESTROY means your evail{} can fail and $@ can be empty afterwards.
> It's why you have to actually return something from eval or assign
> success out somewhere.
>
> For "95%" of perl code, trusting that $@ is an ok boolean works. For
> the other 5%, set phasers to kill and raise all shields.
>
>    my $ok = eval { ...; return 1};
>    unless( $ok ) {
>       my $e = $@;
>       ...
>    }

I tend to write that as an "or":

   eval {
       ...
       1;
   } or {

       my $e = $@;
       printf LOG "[%s] %s\n", scalar localtime, $e;
   };


which could also look more like

   { my @errors;
     push @errors, $@ if $@;
     eval {
         push @errors, $@ if $@;
         ...
         1;
     } or {

         push @errors, $@;
     };
     die @errors if @errors;
   }


-- 
Affijn, Ruud

"Gewoon is een tijger."


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About