[Israel.pm] 2 Qs on assignment and evaluation
oferk at oren.co.il
Thu Jan 29 07:32:52 PST 2004
> You seem to be correct. I tried
> my $a=2;
> print "\n", $a, $a=3, "\n";
> and this results in
> being printed.
> This is very un-C like behaviour! And, in my opinion, non-intitutive.
> my $a=1 ;
> my $b;
> print "\n", $b=$a+1, $a=3, "\n";
> So, this seems to be strange, because in a similar vein as Ofer suggested,
> $b which is first set to 2 (1+1), should be reset to 4 (3+1). But this
> doesn't happen. Why?
> Best regards,
I think the difference between the two examples you gave above are that in
the first you modify the same variable, while in the second example you
modify a different variable, so once $b in this case has been evaluated it
will not change even when $a does. But I'm just guessing here.
If you think the above examples you gave are strange, check this out:
print $a ,$a=3,"\n"; # prints 33
print "$b",$b=3,"\n"; # prints 13
Now for this I really have no explanation, except again to guess that once
the string is evaluated, it does not change (remember that print goes over
the list from left to right). Anyone has any idea if I'm right and where is
I guess the conclusion is, don't modify and use the same variable in the
same statement, you may not get what you think you'll get :-)
More information about the Perl