On Tue, 09 Sep 2014 00:13:13 +0200
lee <[email protected]> wrote:
> Shawn H Corey <[email protected]> writes:
>
> > On Mon, 18 Aug 2014 16:17:53 +0800
> > Ken Peng <[email protected]> wrote:
> >
> >> sub myfunc {
> >> my @x=(1,2,3);
> >> return \@x;
> >> }
> >>
> >> # or,
> >>
> >> sub myfunc {
> >> my @x=(1,2,3);
> >> return [@x];
> >> }
> >
> > # or
> >
> > sub myfunc {
> > return [ 1, 2, 3 ];
> > }
This returns a reference to an anonymous array.
>
> Is there a difference to
>
> sub myfunc {
> return ( 1, 2, 3 );
> }
This returns a list, which can be stored in an array.
>
> ? And my understanding was/is that in
>
> sub myfunc {
> my @x=(1,2,3);
> return \@x;
> }
>
> a reference would be returned to an array that ceases to exist when
> the function returning it has finished. At the point the function
> returns to, there isn't anything left the reference could refer to.
>
> Or is there?
The reference to the array is returned. Perl keeps a reference count on
all the values. With the above return, you create two references to the
value in array @x. When @x goes out of scope, the reference count is
decreased by one. Since it's not zero, the values are returned to the
calling sub.
--
Don't stop where the ink does.
Shawn
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/