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