On Sun Apr 03 16:22:34 2005, perl5-porters@ton.iguana.be wrote: > In article <20050403195113.GH96525@plum._lirble.org>, > Nicholas Clark <nick@ccl4.org> writes: > > On Thu, Mar 17, 2005 at 11:08:00AM -0000, Bram wrote: > > > >> Is this a bug or not? or it the @normal_hash{qx/a b c/}++ an 'accidental > >> feature' which has not been copied to tie? > > > > > > On Tue, Mar 29, 2005 at 05:30:27PM -0600, David Nicol wrote: > > > >> post-increment appears to increment the last element of hash slices > >> but not against > >> arrays in general. Is this by design or laxity? > > > > I assume that being able to increment a hash (or array) slice is a bug, > > given that it's a syntax error on a plain array or hash: > > > For golfers this is a well known fact :-) > > As noted, it works on array slices too: > > perl -wle '@a=(1..4); @a[1..2]++; print for @a' > 1 > 2 > 4 > 4 > > It's also sort of consistent. A slice is is documented to be a list of > values, and in fact the docs are quite explicit about @a[1..2] being > like ($a[1], $a[2]), though the usage of "list" is quite ambiguous > in that part of perldoc perldata. But that's *really* the way it behaves. > > e.g.: > > perl -wle '@a=1..4; print for \@a[1..2]' > > is not some array reference, but: > > SCALAR(0x817350c) > SCALAR(0x8173518) > > just like you'd get from \($a[1], $a[2]) > > And in scalar context such a list of values evaluates to the last value: > > perl -wle '@a=1..4; print for scalar @a[0..2]' > 3 > > So the result is actually as expected and I wouldn't call it a bug. > > PS: > At least until you see that this doesn't work: > > perl -wle '@a=1..4; ($a[1], $a[2])++' > Useless use of array element in void context at -e line 1. > Too many arguments for postincrement (++) at -e line 1, near ")++" > Execution of -e aborted due to compilation errors. > > Maybe THAT'S a bug :-) The ‘too many arguments’ thing is based on the number of operands at a syntactic level. If an expression could (or usually would) return a list, but is still one single term, it gets evaluated in scalar context by operators that propagate scalar context. A hash or array slice in scalar context evaluates to the last element of the slice. $ perl -le' @_ = a..z; print scalar @_[0..5];' f Hash and array slices are valid lvalues. So I think this is not a bug, and that the behaviour is as expected. Changing it would mean adding special cases. -- Father ChrysostomosThread Previous | Thread Next