in reply to Re: Dealing with errors in subroutines
in thread Dealing with errors in subroutines

Somewhere along the way I picked up the habit of writing this idiom so:
for (@users) { my $success = eval { add_account($_); }; if($@ or not $success) { warn "Can't add user '$_': $@" }
But I don't remember why I thought it might be better. Certainly not from readability! What do others think of this kind of double-checking?


- Boldra

Replies are listed 'Best First'.
Re^3: Dealing with errors in subroutines
by moritz (Cardinal) on Sep 14, 2009 at 10:48 UTC
    It's not paranoid but good practice, because (as ELISHEVA pointed out below) a DESTROY method can accidentally reset the global $@ variable, as this small piece of code demonstrates:
    use strict; use warnings; use Data::Dumper; sub DESTROY { eval { 1 } } eval { my $obj = bless {}; die "wrong"; }; print Dumper $@; __END__ $VAR1 = '';

    The correct solution is to localize $@ in the DESTROY sub, but you can't always rely on third-party modules to do such things correctly.

    Perl 6 - links to (nearly) everything that is Perl 6.