develooper Front page | perl.perl5.porters | Postings from September 2003

Re: [docpatch] Re: Order of evaluation of slice and nonslice container accesses

Thread Previous | Thread Next
From:
david nicol
Date:
September 12, 2003 20:45
Subject:
Re: [docpatch] Re: Order of evaluation of slice and nonslice container accesses
Message ID:
1063424709.1486.62.camel@plaza.davidnicol.com
On Wed, 2003-09-10 at 22:11, Enache Adrian wrote:

> > What else?
> 
> This :-)
> 
> $ perl -le 'sub a { print "a" } sub b { print "b" } (a)[b]'
> b
> a
> 
> Regards,
> Adi

so we're up to, in general left-to-right, except the RHS is always
before of the LVALUE, except for self-assignment, when the LVALUE
is part of the RHS and does not get reevaluated.

Also the index of an array is evaluated before a dynamic
array expression, as Adi points out.  Curiously this does not
occur with hashes, only arrays.  

 perl -le 'sub a { print "a" } sub b { print "b" } @{[4 => a]}[b()]'
b
a

 perl -le 'sub a { print "a" } sub b { print "b" } ${{4 => a}}{b()}'
a
b

More specificly, only array slices

 perl -le 'sub a { print "a" } sub b { print "b" } ${[4 => a]}[b()]'
a
b

Hash slices evaluate the slice target first too
 perl -le 'sub a { print "a" } sub b { print "b" } @{{4 => a}}{b()}'
b
a


so 

	(a)[b]	 # a slice, b first
	[a]->[b] # not a slice, a first

Wow.

Is there any sense to this?  The lookup of a sliced container is
not skipped when the key expression returns a null list:
 perl -le 'sub a { print "a" } sub b { print "b";() } (a)[b()]'
b
a

was
that an optimization that was planned but never realized? It would
break some very odd code that considers fetching null slices out of
tied containers significant, it would also break designs that rely
on autovivification in such cases.




-- 
David Nicol /  kernel 2.6.0 is pretty whippy



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About