Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Handling non-fatal method error strings without exceptions or globals?

by itub (Priest)
on Jan 19, 2005 at 16:19 UTC ( #423424=note: print w/ replies, xml ) Need Help??


in reply to Handling non-fatal method error strings without exceptions or globals?

Another option is to have your functions return multiple values:

my ($result, $error) = function($arg); if ($error) { # do something... }

One caveat is that if for some reason you want to disregard the error and only want the result, you need to remember to use parentheses:

($result) = function($arg); # get the result only $result = function($arg); # WRONG!!! gets the error only


Comment on Re: Handling non-fatal method error strings without exceptions or globals?
Select or Download Code
Re^2: Handling non-fatal method error strings without exceptions or globals?
by sleepingsquirrel (Hermit) on Jan 19, 2005 at 16:51 UTC
    Or, you could use wantarray to eliminate the caveat...
    #!/usr/bin/perl -w $result = fun(); print "$result\n"; ($result, $error) = fun(); print "$result, $error\n"; sub fun { return wantarray ? ("stuff","Error") : "stuff" }


    -- All code is 100% tested and functional unless otherwise noted.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-10-01 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls