Re: modifying a variable twice in a statement (comparison to c)



On Feb 4, 10:55 am, "A. Sinan Unur" <1...@xxxxxxxxxxxxxxxxxxx> wrote:
Apparently, perldoc perlop provides the answer in this surprisingly
named section:

Auto-increment and Auto-decrement

"++" and "--" work as in C. That is, if placed before a variable, they
increment or decrement the variable by one before returning the value,
and if placed after, increment or decrement after returning the value.

$i = 0; $j = 0;
print $i++; # prints 0
print ++$j; # prints 1

Note that just as in C, Perl doesn't define when the variable is
incremented or decremented. You just know it will be done sometime
before or after the value is returned. This also means that modifying
a variable twice in the same statement will lead to undefined
behaviour.

I would have expected perl to either reject the syntax or accept as
valid.

In C just because it compiles and runs does not mean it conforms to
the standard, but since in Perl the implementation is the standard, it
makes things tricky when certain things that seem to work in the
reference implementation are actually undefined.

I guess perldoc is like a mini language standard, and if its not in
perldoc then you go by what Perl actually does.

Ivan Novick
http://www.0x4849.net

.



Relevant Pages