So if return; is just as right as return ();, but twice as fast, when would you use return();?

I vaguely remember the meme "use return with no arguments to return false to any context" as received wisdom, but I can't find any such citation.

I would argue that it is sufficiently common usage that it should probably be the default way to signal "false" unless you have good reason otherwise.

If you find yourself in the situation where there are no valid sentinel values to indicate "false" (perhaps you are returning a list where the empty list might be a perfectly valid response) then you need to signal failure some other way. Three come immediately to mind:

  1. Exceptions. This is my preferred technique.
  2. Extra return values. Ugly, but since perl allows for multiple return values, this is workable. (A related technique is passing in a reference to a status variable.)
  3. Global variables. Also ugly. Think errno in C (and how its use forced the rewriting of many calls to make them reentrant, by using the "pass status variable by reference" technique.)

In my own code, I can mostly get away with dealing with empty lists as a normal occurance; if I don't get any answers back, then I just don't do anything. Relying on foreach, map, etc, and avoiding keeping any meta-data about these collections in other variables makes this style straightforward and easy to read.

Exceptions let me write most of my code without worrying about the corner cases; I catch those exceptions towards the top of my code, where I have large chunks of work that I can either accept or reject ("commit" or "rollback", if you like.)

(One thing I would love is if the Fatals module would create detailed exception objects for every call mentioned; currently it is a bit cumbersome to customize the details for each case, so I still have ... or die "..." chunks throughout my code.)


In reply to Re: Functions that return nothing, nada, failure... by tkil
in thread Functions that return nothing, nada, failure... by leriksen

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.