|
Posted by sheinrich on May 18, 2008, 5:12 am
Please log in for more thread options >
> >> [ SNIP ]
>
> It may not be.
>
> > IMHO the expression above can result in an error only, if the
> > increment is first computed and returned and is assigned to $index
> > only after the modulo operation or even after the explicit assignment.
>
> Right. The compiler could produce something like:
>
> $tmp1 = $index + 1
> $tmp2 = $tmp1 % $asize
> $index = $tmp2
> $index = $tmp1
>
> > You may be right in that this can really happen. But then it's a very
> > strange behaviour indeed.
>
> Not that strange for compilers which produce code for real CPUs (which
> often have very strange timing requirements). I wouldn't really expect
> it from a simple bytecode-generating compiler like perl's.
>
> The warning that the execution order is undefined comes from C. But in C
> this is a consequence of the concept of "sequence points". The language
> defines certain points (e.g., the end of each statement, each function
> call, etc.) where each previous operation must be finished and no
> subsequent operation must have begun. between two sequence points the
> compiler can reorder operations at will. There are two rules about
> modifying values:
>
> 1) You can modify an object[1] only once between two sequence points
> 2) If you modify an object, you can only read it to compute the new
> value.
>
> But Perl doesn't have the concept of sequence points, and there is no
> general prohibition agains modifying the same object twice in a
> statement, just against using the increment and decrement operators.
> That does stand out as an odd exception. IMHO Perl should either define
> an execution order (which could be a partial order) or adopt a more
> generic concept like that of C.
>
And the restriction seems particularly odd for a language that
otherwise allows constructs like
@array[$k, $l] = @array[$l, $k];
Reading from the above, it probably wouldn't help to introduce some
strategical brackets, or would it?
$index = (++$index) % $asize;
Thank you,
Steffen
|