Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^5: Elegant way to return true or nothing from a subroutine?

by fenLisesi (Priest)
on Oct 10, 2006 at 12:20 UTC ( [id://577388]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Elegant way to return true or nothing from a subroutine?
in thread Elegant way to return true or nothing from a subroutine?

In the section "Returning Failure," PBP recommends using a bare return to signify failure. There is a two page discussion there, explaining that, at the hands of the caller, the returned undef can easily turn into the one-element list (undef) and evaluate to true.
sub foo { return undef;} sub bar { return;} if (my @foos = foo()) { print "foo() is so true...\n"; ## surprise! } else { print "foo() is false.\n" } if (my @bars = bar()) { print "bar() is true...\n"; } else { print "bar() is false.\n" }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://577388]
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: (7)
As of 2024-04-16 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found