I get the same behaviour with 5.8.4, BTW. It seems that when the range 0..-1 is
used to select the elements of the slice ($#rv is -1 when @rv is
empty), the 'final element' of the slice (incorrectly) evaluates to the previous/last
element on the Perl stack (or some such). With Perl versions up to at
least 5.8.4, that is — but no longer with 5.8.8 and 5.10.0 (I currently
don't have access to versions 5.8.5 - 5.8.7, so I can't tell when it got fixed).
(Ranges like [99..98] behave the same way as [0..-1], so
what seems to matter is just that the second value in the range is
smaller than the first...)
my @x = ('A','B');
print "-------------------\n";
print "[foo", scalar(@x[0..2]), "]\n";
print "-------------------\n";
print "[foo", scalar(@x[0..1]), "]\n";
print "-------------------\n";
print "[foo", scalar(@x[0..0]), "]\n";
print "-------------------\n";
print "[foo", scalar(@x[0..-1]), "]\n";
print "-------------------\n";
With 5.8.4 (and earlier), this prints
-------------------
Use of uninitialized value in print at ./663945.pl line 9.
[foo]
-------------------
[fooB]
-------------------
[fooA]
-------------------
[foo[foo]
-------------------
and with 5.8.8 or 5.10.0
-------------------
Use of uninitialized value in print at ./663945.pl line 9.
[foo]
-------------------
[fooB]
-------------------
[fooA]
-------------------
Use of uninitialized value in print at ./663945.pl line 15.
[foo]
-------------------
As has already been pointed out elsewhere in the thread, this is
mostly expected behaviour, because (from perldoc -f scalar)
scalar EXPR
(...)
Because "scalar" is unary operator, if you accidentally use for EXPR
a parenthesized list, this behaves as a scalar comma expression,
evaluating all but the last element in void context and returning the
final element evaluated in scalar context.
...except for the "[foo[foo]", of course.
I don't have a real explanation (probably simply a bug)...
just a couple of related observations with respect to using [0..-1] with
slices. When you use a literal list instead of a named array, there's
still some curious behaviour in recent releases of Perl:
print "-------------------\n";
print "[foo", scalar(('A','B')[0..2]), "]\n";
print "-------------------\n";
print "[foo", scalar(('A','B')[0..1]), "]\n";
print "-------------------\n";
print "[foo", scalar(('A','B')[0..0]), "]\n";
print "-------------------\n";
print "[foo", scalar(('A','B')[0..-1]), "]\n";
print "-------------------\n";
prints
-------------------
Use of uninitialized value in print at ./663945.pl line 26.
[foo]
-------------------
[fooB]
-------------------
[fooA]
-------------------
Argument "[foo" isn't numeric in list slice at ./663945.pl line 32.
[fooA]
-------------------
The '"[foo" isn't numeric...' seems to suggest that with
[0..-1] the value "[foo" is being used to index the element from
the list... which is confirmed by this:
print "-------------------\n";
print 2, scalar(('A','B')[0..-1]), "]\n"; # elem at index 2 (undef)
print "-------------------\n";
print 1, scalar(('A','B')[0..-1]), "]\n"; # elem at index 1 ('B')
print "-------------------\n";
print 0, scalar(('A','B')[0..-1]), "]\n"; # elem at index 0 ('A')
print "-------------------\n";
print -1, scalar(('A','B')[0..-1]), "]\n"; # elem at index -1 (last e
+lem 'B')
print "-------------------\n";
print -2, scalar(('A','B')[0..-1]), "]\n"; # elem at index -2 ('A')
print "-------------------\n";
print -3, scalar(('A','B')[0..-1]), "]\n"; # elem at index -3 (undef)
print "-------------------\n";
which prints
-------------------
Use of uninitialized value in print at ./663945.pl line 37.
2]
-------------------
1B]
-------------------
0A]
-------------------
-1B]
-------------------
-2A]
-------------------
Use of uninitialized value in print at ./663945.pl line 47.
-3]
-------------------
Looks like this "indirect indexing" feature could be useful for obfus ;)
|