In the C language what the debate is about is clearly: UNDEFINED.
Java works deliberately DIFFERENTLY, it enforces a predictable sequence.

I think in Perl, the outcome is also UNDEFINED (as Mr. Jay already said).

I presume the Perl language acts here just as C. It is pointless to argue about order and whatsoever, unless you know the given language STANDARD back and forth. It is a language specific issue, so don't come with saying "++x increments, then returns, x++ returns "then" increments, so the outcome is xyz, regardless of order blah, blah..." until you KNOW for SURE what the language says about the issue (currently: referencing an incremented variable in the same expression).

I strongly suggest everyone to read this article (although it is for C, I think the same applies to Perl):

http://c-faq.com/expr/evalorder2.html

Other nice questions:

http://c-faq.com/expr/


For a little fun, I wrote a little C program and a Perl equivalent.


#include <stdio.h>

int main()
{
        int x;
        x = 0; printf("%d\n",++x + ++x);
        x = 0; printf("%d\n",x++ + x++);
        x = 0; printf("%d\n",++x + x++);
        x = 0; printf("%d\n",x++ + ++x);
        
        return 0;
}


gcc -Wall -o test.exe test.c
test.c: In function `main':
test.c:6: warning: operation on `x' may be undefined
test.c:7: warning: operation on `x' may be undefined
test.c:8: warning: operation on `x' may be undefined
test.c:9: warning: operation on `x' may be undefined

test.exe
4
0
2
2

The result is the same with MSVC 2003 compiler.


And in Perl:


use strict;
my $x;

$x = 0; printf("%d\n", ++$x + ++$x);
$x = 0; printf("%d\n", $x++ + $x++);
$x = 0; printf("%d\n", ++$x + $x++);
$x = 0; printf("%d\n", $x++ + ++$x);


perl test.pl
4
1
3
2

--
Greets,
B.

P.S.: As to the C program, that the original author posted: the outcome of that is also UNDEFINED. It was mere luck or chance, that the result was 8. GCC issues a warning for that too.
#include <stdio.h>

int main()
{
	int x;
	x = 0; printf("%d\n",++x + ++x);
	x = 0; printf("%d\n",x++ + x++);
	x = 0; printf("%d\n",++x + x++);
	x = 0; printf("%d\n",x++ + ++x);
	
	return 0;
}
use strict;
my $x;

$x = 0; printf("%d\n", ++$x + ++$x);
$x = 0; printf("%d\n", $x++ + $x++);
$x = 0; printf("%d\n", ++$x + $x++);
$x = 0; printf("%d\n", $x++ + ++$x);

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to