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

Re: Multiple post(inc|dec)rement

Thread Previous
Nicholas Clark
January 17, 2001 06:33
Re: Multiple post(inc|dec)rement
Message ID:
On Tue, Jan 16, 2001 at 04:39:24PM -0800, Stephen McCamant wrote:
> >>>>> "GNat" == Nathan Torkington <> writes:
> GNat> Johan Vromans writes:
> JV> My point is that unlogical exceptions like @a = (++$i,++$i,++$i)
> JV> are hard to explain, and make people turn away from Perl.
> GNat> Could the optimizer spot multiple side-effects in an expression
> GNat> and pessimize it accordingly?
> If you mean a syntactic expression, then yes, I think that would be
> possible. In general, though, side effects might be hidden in
> subroutines, AUTOLOADed subroutines, evals, etc:
> perl -e 'sub f {++$i}; print ++$i, f(), "\n"'
> It doesn't seem obvious to me that there's an easy or foolproof way to 
> do this, though, and a half-right solution would probably be worse
> that a the current situation, at least as far as being predictable and 
> explainable.

I agree that I can't se an easy or foolproof way to cope with eval,
subroutines etc. I suspect it's not even possible.

However, I'm not sure if a "half right" solution might be quite doable
(With the peephole pessimiser adding temporaries to make it work correctly)
and documentable

  For speed reasons perl doesn't guarantee that it will evaluate lists
  in any particular way (eg left to right, or right to left). However, if
  at compile time it can see accesses to the same variable in a single
  expression, for example

    @a = (++$i,++$i,++$i);
    &g(++$i, $i);

  then it will "do what I mean" and make special preparations to ensure that
  @a is set to (1 ,2, 3)
  This check is only for terms in a single expression, and doesn't descend
  into subroutines or evals. So

    sub f {return ++$i}

    &g ((f(), ++$i, eval {$i}));
    @b = (f(), f(), f());

  will not be treated specially, and the arguments will be evaluated in
  whichever order is most convenient to the perl implementation.

Does this make enough sense to fly?

[no, I don't know how to write the patch to op.c]

Nicholas Clark

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About