in reply to Question on "Effective Perl Programming"
I personally never test $@ at all. I've seen way too many practical examples of eval failing and yet $@ being left unset so I don't consider using $@ to test whether an eval failed to be wise.
A more robust approach is:
my $okay= eval { ....; 1 }; if( ! $okay ) { warn "Error in eval: $@\n"; }
Here is a quick example of how to make eval fail and leave $@ unset:
sub DESTROY { # local $@; # Uncomment this line to "fix" this eval { # Something that might fail # And failure should just be ignored here }; } my $okay= eval { my $x= bless []; die "The world is coming to an end!!!\n"; 1 }; if( $@ ) { warn "eval failed obviously: $@\n"; } elsif( ! $okay ) { warn "eval failed silently ($@).\n"; } else { warn "eval succeeded.\n"; }
which produces "eval failed silently ()." for me.
I'm a bit disappointed that the authors of "Effective Perl Programming" neglected this technique. You might ask them to have that noted in the errata.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question on "Effective Perl Programming" (;1 > $@)
by kyle (Abbot) on Sep 06, 2007 at 14:30 UTC | |
by tye (Sage) on Sep 06, 2007 at 14:43 UTC | |
by Anonymous Monk on Sep 07, 2007 at 13:11 UTC | |
by girarde (Hermit) on Sep 08, 2007 at 21:03 UTC | |
by runrig (Abbot) on Sep 08, 2007 at 21:22 UTC | |
by kyle (Abbot) on Sep 08, 2007 at 21:39 UTC | |
|