in reply to Re: Sometimes it's in Void Context
in thread Sometimes it's in Void Context
Heh :) Even though your answering a totally different question, it does raise another issue. Why are the following three cases treated differently?
$lines[0] = [ 3,2,1 ]; print $lines[0][ 3,2,1 ]; print @{$lines[0]}[ 3,2,1 ];
Ignoring, for the moment, the "Useless use of constant in void context" warnings, how does Perl know that the first use of [ 3,2,1 ] is to generate a reference to an anonymous array from a list, while the second example doesn't reduce to this:
print $lines[ARRAY(0x1a7efd8)];
We have the same issue with the third example recognizing that [ 3,2,1 ] is to be used as a list of array indices for the slice.
This behavior should is difficult to keep straight:
$lines[0] = [ "dog", "cat" ];
Why doesn't that just assign "cat" to the first element of @lines? My assumption is that it is because the square brackets are performing a subtle double-duty. In this example:
print $lines[0][0,1];
the square brackets are being used to indicate an array element. In another example:
$lines[0] = [ "dog", "cat" ];
the square brackets are being used to create a reference to an anonymous array and constructs the anonymous array from a list. Keeping things straight can be a major headache, which is why I'm glad Perl does it for me :)
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Sometimes it's in Void Context
by JayBonci (Curate) on Jun 01, 2002 at 02:01 UTC | |
by Aristotle (Chancellor) on Jun 01, 2002 at 18:29 UTC | |
by JayBonci (Curate) on Jun 01, 2002 at 19:03 UTC | |
Re: Re: Re: Sometimes it's in Void Context
by traveler (Parson) on Jun 01, 2002 at 17:28 UTC | |
Re: Re: Re: Sometimes it's in Void Context
by ariels (Curate) on Jun 03, 2002 at 08:45 UTC | |
by theorbtwo (Prior) on Jun 03, 2002 at 08:52 UTC |