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

Srikanth Madani srikanth.madani at vodafone.com
Thu Jan 29 03:17:01 PST 2004


I got the C code wrong. It should be:

__C code starts__
#include<stdio.h>
main (void) {
        int res, a;
        res=(a=2);
        printf ("\nThe value of res is %d, and a is %d.\n", res, a);
        printf ("\nres is %d, and res is %d.\n", res=2, res++);
}
__C code ends__

which produces:

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

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


Why does Perl behave differently (well, in this case, I mean!) ?

Best regards,
Srikanth

-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il]On Behalf Of
Srikanth Madani
Sent: Thursday, January 29, 2004 10:36 AM
To: Perl in Israel
Subject: [Israel.pm] RE: 2 Qs on assignment and evaluation



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____

produces:

__perl output starts__
res is 3.
32
__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__
#include<stdio.h>
main (void) {
        int res, a;
        res=(a=2);
        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,
Srikanth



_______________________________________________
Perl mailing list
Perl at perl.org.il
http://www.perl.org.il/mailman/listinfo/perl

YAPC::Israel::2004
http://www.perl.org.il/YAPC/2004/




More information about the Perl mailing list