On Mon, Aug 18, 2014 at 1:38 AM, Paul Johnson <p...@pjcj.net> wrote:
> On Mon, Aug 18, 2014 at 04:17:53PM +0800, Ken Peng wrote:

>> which one is the better way to return the list content? And if the
>> method is an instance method?
>
> Functionally, the two are identical.  The first is returning a reference
> to the array you have created.  The second is returning a reference to a
> new array created from a (shallow) copy of the array you have created.

[ snip ]

To expand further (because you asked about "list context"), note that
both are returning a _scalar_, because references are always scalars.
If you want to return a list, you should just do

    return @x;

If you wanted the return value to depend on the calling context, you
could use 'wantarray' like so:

    return wantarray ? @x : \@x;

That said, APIs that have contextually dependent return values are
usually fairly confusing to use, so consider not doing this.

chrs,
john.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to