On Dec 11, 10:52 am, rvtol+use...@isolution.nl (Dr.Ruud) wrote:
> C.DeRykus wrote:
> > Dr.Ruud:
> >> C.DeRykus:
> >>>      eval { asub() };
> >>>      die $@ if $@;
>
> >> You need to test the return of eval itself to be sure.
>
>   ...
> perl -wle '
>
>    die "An error: ", $@ || "whoopy"
>      if !eval{ asub(); 1 };
>
>    sub asub{ my $x = bless {}, "main"; my $y = 0; 1 / $y }
>
>    sub DESTROY{ my $z = 0; 1 / $z }
> '
>         (in cleanup) Illegal division by zero at -e line 8.
> An error: Illegal division by zero at -e line 6.
>         (in cleanup) Illegal division by zero at -e line 8.
>
> The size of the eval block has nothing to do with it.
> There is just no other sound way to handle exceptions in Perl.
>
> Never rely on a global variable, it can get changed.
> Localizing $@ is another way, but that hides errors.
>
> Code that might change $@ inside a DESTROY,
> can politely push it to @@ first:
>
>      push @@, $@ if $@;
>
> because then you can also check $...@}, $...@[-1], etc.


That's very good info and robust exception handling is always
the way to go. Even more important if the code is dynamic.
A short example of a one-liner sub may fly under the radar
without too much flak but the right way should always be
noted.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to