Bug#256865: perl: (@a)[5,3] != @a[5,3]

Niko Tyni ntyni at debian.org
Sat May 2 19:32:32 UTC 2009


On Tue, Jun 29, 2004 at 04:58:17PM +0200, Peter Palfrader wrote:
> Package: perl
> Version: 5.8.4-2
> Severity: normal

> I assumed that when slicing arrays, that
>         @a[5,3]
> and
>         (@a)[5,3]
> would yield the same result.
> 
> It does, if at least one values indexed in @a is defined, but it doesn't if
> they all are not:

The important difference here is that @a[5,3] is an array slice while
(@a)[5,3] is a list slice.

An array slice is equivalent to a list of the individually subscripted
elements, here ($a{5}, $a{3}). From perldata.pod:

           @days[3,4,5]        # same as ($days[3],$days[4],$days[5])

so the resulting '(undef, undef)' in your example is clearly working
as documented for this case.

However, list slices are documented to work a bit different:

       A slice of an empty list is still an empty list.  Thus:

           @a = ()[1,0];           # @a has no elements
           @b = (@a)[0,1];         # @b has no elements
           @c = (0,1)[2,3];        # @c has no elements

       But:

           @a = (1)[1,0];          # @a has two elements
           @b = (1,undef)[1,0,2];  # @b has three elements

This seems to cover your example, although it could certainly be clearer
as three of the five examples refer to a slice of a non-empty list with
some or all indices out of range.

Also see <http://rt.perl.org/rt3/Public/Bug/Display.html?id=39882>, which
shows an exception to the above (in the context of function arguments)
treated as a bug and fixed for 5.8.9 and 5.10.0. Specifically:

% perl -e 'sub nr { scalar @_ }; @a=(1); printf "perl $]: list slice length %d array slice length %d\n", nr((@a)[2,3]), nr(@a[2,3])'                                                                       

on Etch gives

perl 5.008008: list slice length 2 array slice length 2

while on Lenny it's 

perl 5.010000: list slice length 0 array slice length 2

> If this is indeed the correct behaviour, please excuse my ignorance.

It seems to be working as intended. Would you like to turn this bug into
a request for improved documentation or shall we just close it?

Cheers,
-- 
Niko Tyni   ntyni at debian.org






More information about the Perl-maintainers mailing list