Your making an assumption that turns out to be wrong :-)

First:

sub y { \&x(); }
Returns a reference to whatever x() returns *) but functions can not return plain arrays; they returns lists or single (scalar) values, depending on how they're called (this is called context in perl).

update: the following is wrong, since \ forces list-context on its argument

If x() would return a list, y() would return a list of references to those values, but that's not what is happening here.

Since you're doing

my $h = &y;
y() is called in scalar (in stead of list) context. the context is propagated to the returning statement in y() - that's \&x();. That propagates the scalar context up to the my @a = (1,2,3) statement. That statement is a list assignment, and a list assignment in scalar context returns the number of values assigned.

So, x() returns 3, and then you take a reference to that and return it from y() and assign it to $h.

Then you try to derefence $h as an array reference, but since $h contains a reference to a scalar, that won't work.

update: the rest is correct, though

The quickest fix would be:

sub y { [ x() ]; }

Which creates a new anynomous array containing all the values returned by x() in list context.

*) Personally I think using \&sub(); is really confusing, since \⊂ without parens returns a reference to the subroutine; generally I think it's better to never use &sub(); for calling subroutines, since ⊂ can also cause confusion - see perlsub. In essence, you only need & for taking a reference to a sub, or other special cases, and you probably should use sub(); ( or sub; ) for all "normal" cases.


In reply to Re: return a ref to a return value (array) of another sub by Joost
in thread return a ref to a return value (array) of another sub by telcontar

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.