Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Returning zero in Perl is usually a bad meme. It is ok if you actually meant to return a numeric zero, but 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. The code would then look like this:

The opposite could be argued just as easily: if you return an empty list to mean "false", the false-ish-ness could be lost (or other problems could arise) if your return value is "slurped" into another list context.

Consider...

#!/usr/local/bin/perl -wl use strict; sub a_func_that_returns_false { return 0; } sub your_func_that_returns_false { return (); } sub func_that_expects_string_boolean_and_num { my $str = shift; my $bool = shift; my $num = shift; print "CALLED FUNC: ", $str; print " bool was ", ($bool ? "true" : "false"); print " num was ", (20 < $num ? "bigger" : "smaller"), " then tw +enty"; } &func_that_expects_string_boolean_and_num ("the zero way", &a_func_that_returns_false(), 42); &func_that_expects_string_boolean_and_num ("the empty list way", &your_func_that_returns_false(), 42); __DATA__ laptop:~> monk.pl CALLED FUNC: the zero way bool was false num was bigger then twenty CALLED FUNC: the empty list way bool was true Use of uninitialized value in numeric lt (<) at monk.pl line 12. num was smaller then twenty

Bottom line: there's no substite for using wantarray to check your calling context, and acting appropriately (except perhaps extensively documenting that your method may not work the way people think and making it their responsibility to force the calling context)

Update: good point blakem


In reply to Re: Re: variable set to 0 ? 0 : 1 by hossman
in thread variable set to 0 ? 0 : 1 by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-23 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found