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

Re: Multiple post(inc|dec)rement

Thread Previous | Thread Next
Stephen McCamant
January 15, 2001 17:57
Re: Multiple post(inc|dec)rement
Message ID:
>>>>> "SC" == Simon Cozens <> 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> +    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"'


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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About