On Mon, Aug 18, 2014 at 04:17:53PM +0800, Ken Peng wrote:
> Hello,
> 
> sub myfunc {
>   my @x=(1,2,3);
>   return \@x;
> }
> 
> # or,
> 
> sub myfunc {
>   my @x=(1,2,3);
>   return [@x];
> }
> 
> 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.

The fist is to be preferred in my opinion, because it does exactly what
you want.  The second does extra work and might cause someone to wonder
why you haven't just returned a reference to the array.

The second version is necessary when the array might persist between
subroutine calls and you effectively need to return a copy.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
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