in reply to Elegant way to return true or nothing from a subroutine?

return; does not return "nothing", as the documentation clearly states:

       return EXPR
       return  Returns from a subroutine, "eval", or "do FILE" with the value
               given in EXPR.  Evaluation of EXPR may be in list, scalar, or
               void context, depending on how the return value will be used,
               and the context may vary from one execution to the next (see
               "wantarray").  If no EXPR is given, returns an empty list in
               list context, the undefined value in scalar context, and (of
               course) nothing at all in a void context.                        

So the equivalent to simply return; is return wantarray ? () : undef.


We're not surrounded, we're in a target-rich environment!

Replies are listed 'Best First'.
Re^2: Elegant way to return true or nothing from a subroutine?
by Limbic~Region (Chancellor) on Oct 10, 2006 at 13:10 UTC
    jasonk,
    Actually, the documentation clearly states it does return nothing provided the sub was called in a void context. Unfortunately there isn't a clear way to say:
    return defined(wantarray) ? wantarray ? () : undef : NOTHING;

    Cheers - L~R

      As fortunately, this never matters, because by definition nobody gets to look at the return value in void context.