I'm wanting it to return a list so I can measure its size.

Not quite. The best thing I like to say to start out with in these discussions is "there is no such thing as a list in scalar context." The result you're going for is that an array in scalar context returns its number of elements. And it is precisely the purpose of the function "scalar" to apply scalar context to the expression it's being called on. No before or after about it. But I should think that something like

scalar @{[ $obj->link('next') ]}
ought to work. There's a few things going on here. The inner brackets do two things: a) force the link method to be called in list context, and b) form an anonymous array out of the results. But the return value of the brackets is just a reference to the array. What you really need is the array itself in scalar context, as if it were a "real" array variable with a name and everything. That's what the @ sigil and the outer braces are doing. They're taking this array reference you just created and de-referencing it as a "real" array which, when invoked in scalar context, returns its size. The reason that
scalar @{ $obj->link('next') }
doesn't work is that the expression inside the braces is evaluated in scalar context, and the result is expected to be either an array reference or an identifier (in which case it becomes a symbolic rerefence to the array of that name in the current package). Fun, huh?

Update: Of course, if what the method does in scalar context is return an array reference, then the second construct will work fine also.


In reply to Re: Confused Contexts and wantarray by Errto
in thread Confused Contexts and wantarray by Dr. Mu

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.