Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Dealing with errors in subroutines

by Boldra (Deacon)
on Sep 14, 2009 at 07:57 UTC ( [id://795076]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://795076]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found