develooper Front page | perl.perl5.porters | Postings from June 2004

[PATCH pod/perlop.pod] Documenting undefined behaviour of $i = $i ++.

Thread Next
From:
Abigail
Date:
June 30, 2004 04:21
Subject:
[PATCH pod/perlop.pod] Documenting undefined behaviour of $i = $i ++.
Message ID:
20040630100021.GA23752@abigail.nl

Modifying a variable twice using ++ (or --) in the same statement seems
to be a never ending cause of confusion. This patch will document it as
being undefined behaviour.


Abigail


--- pod/perlop.pod.orig	Wed Jun 30 11:52:00 2004
+++ pod/perlop.pod	Wed Jun 30 11:56:35 2004
@@ -144,6 +144,17 @@
     print $i++;  # prints 0
     print ++$j;  # prints 1
 
+Note that just as in C, Perl doesn't define B<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.
+Avoid statements like:
+
+    $i = $i ++;
+    print ++ $i + $i ++;
+
+Perl will not guarantee what the result of the above statements is.
+
 The auto-increment operator has a little extra builtin magic to it.  If
 you increment a variable that is numeric, or that has ever been used in
 a numeric context, you get a normal increment.  If, however, the

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