Jim Gibson <[email protected]> writes:
> On Sep 8, 2014, at 3:13 PM, lee wrote:
>
>> Shawn H Corey <[email protected]> writes:
>>
>>> On Mon, 18 Aug 2014 16:17:53 +0800
>>> # or
>>>
>>> sub myfunc {
>>> return [ 1, 2, 3 ];
>>> }
>>
>> Is there a difference to
>>
>> sub myfunc {
>> return ( 1, 2, 3 );
>> }
>
> The first example returns [ 1, 2, 3 ], which is a reference to a list with 3
> elements. References are scalars.
>
> The second example returns ( 1, 2, 3 ), which is a list with 3 elements.
>
> So, yes, there is a difference.
Hm, so what way of thinking is behind this, and how do you declare an
array?
my $i = 1;
my $f = 2.5;
my $s = 'string';
my $list = (1, 2, 3);
my $list_reference = [(1, 2, 3)];
my $dereferenced_list = $@list_reference;
my @artificial_array = $@list_reference;
my @true_array = ?
I'm finding this very confusing. What's the benefit of using extra
designators for some types of variables (arrays) while not even having
any at all for some others (lists)?
>> ? 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?
>
> Perl will not garbage-collect an element if there is a reference to
> it. So while the array name @x no longer is in scope once the
> subroutine has returned, the list that was the value of @x still
> exists in memory, and the reference returned by the subroutine will be
> valid.
>
> If the reference is not saved or copied, then the list will eventually get
> garbage collected.
Hmm, ok, strange ... good to know, though.
--
Knowledge is volatile and fluid. Software is power.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/