On Dec 10, 1:21 pm, rvtol+use...@isolution.nl (Dr.Ruud) wrote:
> C.DeRykus wrote:
> > On Dec 8, 1:57 am, dery...@gmail.com (C.DeRykus) wrote:
> >> On Dec 8, 12:08 am, an...@melerit.se (Anders Hartman) wrote:
> >> Also, in this case, I'd write the eval for compile-time and check
> >> for errors:
>
> >>      eval { asub() };
> >>      die $@ if $@;
>
> > No, sorry,  that's a "useless use of eval" since you could just call
> > the sub directly.  Since, the intent was to define and call the sub
> > dynamically though, checking for eval errors is still a good idea.
>
> You need to test the return of eval itself to be sure.
>
>      eval {
>          asub();
>          1;  # success
>      }
>      or do {
>          my $error = $@ || "unknown";
>          die $error if $error;
>      };
>
> Alternatively:
>
>      die $@||"unknown" if !eval{asub();1};
>
> Example:
>
> perl -wle '
>
>    die "An error: ", $@ || "whoopy"
>      if !eval{ asub(); 1 };
>
>    sub asub{ my $x = bless {}, "main"; 1 / 0 }
>
>    sub DESTROY{ $@ = "bad mojo" }
> '

Good reminder... particularly if the eval block gets long.

--
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