[Israel.pm] RE: 2 Qs on assignment and evaluation

Srikanth Madani srikanth.madani at vodafone.com
Thu Jan 29 02:35:44 PST 2004

I posted this yesterday, but I think it was somehow corrupted, so here goes:

Hi all,

I guess these are basic questions, though I saw a similar query on another
list and so perhaps I'm not the only one.

The following snippet:

__perl code starts__
#!/usr/local/bin/perl -w
use strict;
use diagnostics;
my $res = my $a = 3;
print "\nres is $res.\n";
print $a=2, $a++, "\n";
_perl code ends____


__perl output starts__
res is 3.
__perl output ends___

Q1. Shouldn't $res have the value of the _assignment operation_ (usually 1,I
guess) and not the value which $a is assigned to?

Q2. Shouldn't the second print produce 23 instead of 32?

I tried the same thing in C, and got exactly the same results:

__C code starts__
main (void) {
        int res, a;
        printf ("\nThe value of res is %d, and a is %d.\n", res, a);
        printf ("\nres is %d, and res is %d.\n", res, res++);
__C code ends__

__C output starts__
The value of res is 2, and a is 2.

res is 3, and res is 2.
__C output ends___

Thanks for your help,

More information about the Perl mailing list