develooper Front page | perl.perl5.porters | Postings from January 2001

Re: Multiple post(inc|dec)rement

Thread Previous | Thread Next
From:
Stephen McCamant
Date:
January 15, 2001 17:57
Subject:
Re: Multiple post(inc|dec)rement
Message ID:
14947.42375.200549.530587@soda.csua.berkeley.edu
>>>>> "SC" == Simon Cozens <simon@cozens.net> writes:

SC> On Mon, Jan 15, 2001 at 09:16:22AM +0100, Johan Vromans wrote:

JV> But I still think that
JV> perl -e '@a = (++$i,++$i,++$i); print "@a\n"'
JV> should print "1 2 3".

SC> Oh, you should have said. :)

[change to pp_predec snipped]

SC> --- pp_hot.c~	Mon Jan 15 09:00:35 2001
SC> +++ pp_hot.c	Mon Jan 15 09:05:32 2001
SC> @@ -314,6 +314,7 @@ PP(pp_preinc)
SC>      else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
SC>  	sv_inc(TOPs);
SC>      SvSETMAGIC(TOPs);
SC> +    TOPs = sv_2mortal(newSVsv(TOPs));
SC>      return NORMAL;
SC>  }
 
SC> I'll leave adding tests until people have decided whether or not
SC> this is appropriate/correct.

IMHO, pp_pre(inc|dec) were written the way they are now for a reason
(namely efficiency), and it isn't worth slowing them down just to make 
some pathological examples more intuitive. If you want "1 2 3", you
can always write

perl -e '@a = (++$i+0, ++$i+0, ++$i+0); print "@a\n"'

or

perl -e '@a = (do{++$i}, do{++$i}, do{++$i}); print "@a\n"'

If you really wanted to put this class of surprises to rest
permanently, the place to make mortal copies would be in pp_gvsv and
pp_padsv (conditionalized on !(OPf_MOD | OPf_REF)), though that of
course would make things even slower.

If you are going to make a copy, though, it would be better to do it
with a target, like post(inc|dec). And you could remove the code in
scalarvoid() that optimizes post-(in|de)crements to
pre-(in|de)crements, since they'd be the same speed.

 -- Stephen McC


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