[Israel.pm] 2 Qs on assignment and evaluation

Srikanth Madani srikanth.madani at vodafone.com
Thu Jan 29 04:29:30 PST 2004

Ofer Kaye wrote:

>So consider what happened- for print to work, it first has to evaluate the
>list of arguments.
>The first, "$a=2", does just what you expect, setting $a to 2.
>The next, $a++, *first* returns 2, *then* increases $a to 3.
>Finally, "\n" is "\n", so print is finished. However, $a was changed, so
>final output is:
>Which is what you got. QED :-)

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,

In the beginning there was data.  The data was without form and
null, and darkness was upon the face of the console; and the Spirit of
IBM was moving over the face of the market.  And DEC said, "Let there
be registers"; and there were registers.  And DEC saw that they
carried; and DEC separated the data from the instructions.  DEC called
the data Stack, and the instructions they called Code.  And there was
evening and there was morning, one interrupt.
                -- Rico Tudor, "The Story of Creation or, The Myth of Urk"

More information about the Perl mailing list