in reply to Re: sub returning undef
in thread sub returning undef

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

Replies are listed 'Best First'.
Re: sub returning undef
by Abigail-II (Bishop) on May 24, 2004 at 16:10 UTC
    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