Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re(2): variable set to 0 ? 0 : 1

by Arien (Pilgrim)
on Sep 06, 2002 at 07:42 UTC ( [id://195591]=note: print w/replies, xml ) Need Help??


in reply to Re: variable set to 0 ? 0 : 1
in thread variable set to 0 ? 0 : 1

... if you mean to return "false", the Perlish way is to return an empty list. This is because by returning 0 you return a single-element list, which evaluates to true in list context.

I feel the risk of that happening is mildly exaggerated. :-)

If you directly evaluate the return value of the sub for truth you supply the sub a Boolean context, and there will be no problem.

To run into problems, you'd have to call the sub in a list context, save the list of 1 return value and then evaluate that in scalar context. But then the problem would be evaluating a list in scalar context when you didn't mean to.

— Arien

Replies are listed 'Best First'.
Re^3: variable set to 0 ? 0 : 1
by Aristotle (Chancellor) on Sep 06, 2002 at 13:46 UTC
    The risk of that happening may be small, but it's a question of principle and good habit. Specifically, a question of principle of least surprise. If you simply return when you mean to return "false", your code will do the right thing in most cases without requiring hoop jumps on the part of the caller. There are obviously reasons to deviate from the standard idiom in specific cases, but you should be aware what the standard idiom is and why it is that; so that you know when and when not to apply it.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found