in reply to sub returning undef

sub never return arrays. They return lists. If you do return undef a list of one element is returned. If you assign that to a scalar, the scalar will have the undefined value. If you assign it to an array, you get an array with one element, that element being undef. It's not any different from return "feebleprops".

What is special cased is return;, that is, a return without arguments. In scalar context, that will return an undefined value - in list context, it will return an empty list. This is done especially to help in cases you are using.

Abigail

Replies are listed 'Best First'.
Re: sub returning undef
by jonadab (Parson) on May 24, 2004 at 15:32 UTC
    sub never return arrays. They return lists.

    A sub never returns an array _directly_, but it can return arrays by reference, along with anything else it wants to return, in its list of return values.

    The OP may have meant that the sub returns a list of whatevers, or he might have meant that it returns an array reference. It's hard to be certain since he only gave us a possibly-misleading comment for that case and focused on the error case where he returns undef. My guess is, he hasn't _written_ the non-error case yet and hasn't put a lot of thought into how it returns, but that's only a guess.


    ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print
      A sub never returns an array _directly_, but it can return arrays by reference, along with anything else it wants to return, in its list of return values.
      Well, duh. That's like responding to someone who says "you can't make trees with a camera, just pictures" with "but you can make a picture of a tree!".

      The whole problem of the OP deals with the fact subs return lists, not arrays. The fact that a list element can be a reference to an array is an interesting observation in its own right - but not for this discussion.

      The OP may have meant that the sub returns a list of whatevers, or he might have meant that it returns an array reference.
      The code never uses references. Both the comment and the code show that the OP expected @array to be empty. It's pretty clear to me what the OP expected, and where the OP went wrong.

      Abigail