On Sep 9, 2014, at 2:09 PM, lee wrote: > Jim Gibson <jimsgib...@gmail.com> writes: > >> On Sep 8, 2014, at 3:13 PM, lee wrote: >> >>> Shawn H Corey <shawnhco...@gmail.com> 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?
Perls 1-4 (I believe) did not have references. They were added with Perl 5, which allowed complex data structures not possible without them. Think array of arrays and hashes of hashes. Once references were introduced, a lot of new possibilities for passing arguments and returning values arose. > my $i = 1; > my $f = 2.5; > my $s = 'string'; > my $list = (1, 2, 3); That should be: my @list = ( 1, 2, 3 ); Some people make a distinction between 'list' and 'array'. A list is a sequence of scalar values indexed by an integer. An array is a variable whose value is a list (or something like that -- I don't really distinguish the two myself.) > my $list_reference = [(1, 2, 3)]; That can be expressed more simply: my $list_reference = [ 1, 2, 3 ]; > my $dereferenced_list = $@list_reference; That is incorrect. You probably mean either this: my @dereferenced_list = @$list_reference; or this: my $list_reference = \@list; or even this: my $list_reference = \(1, 2, 3); > my @artificial_array = $@list_reference; There is nothing artificial about that array: my @array = @$list_reference; > my @true_array = ? my @true_array = ( 1, 2, 3 ); In either case, the result is the same. @array and @true_array are both arrays with a set of values. > > > 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)? The designators are for named variables. Lists are like pure values, just like 1.0 and "string", Like all literal values, they do not need designators (names). >> 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. Not strange at all. If Perl knows there is no way to reference or access a value in memory, that value will get garbage collected. If there is a way, it won't. To do otherwise would either 1) leak memory, or 2) lead to invalid references. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/