SPUG: Behavior of &&'ed functions
John W. Krahn
krahnj at telus.net
Wed Oct 27 16:07:11 CDT 2004
John Costello wrote:
> I have some code which boils down to the example shown below. I expected
> to see four lines of output, with $column incremented twice. Instead, the
> first print statement in each block does not output. Instead, the second
> and third lines are performed and apparently the first print outputs the
> boolean evaluation of &&'ed statements. Why does Perl (5.8.0) behave this
> Enlightenment appreciated.
> #!/localdepot/perl-5.80/bin/perl -w
> # test6.pl
> use strict;
> my ($column);
> $column = 0;
> print "column is: $column\n"
> && ++$column
> && print "now column is: $column\n";
> print "and column is: $column\n"
> && $column++
> && print "and now column is: $column\n";
> now column is: 1
> 1and now column is: 2
To see what is happening use the B::Deparse module.
$ perl -MO=Deparse,-p -e'print "column is: $column\n" && ++$column && print
"now column is: $column\n";'
print((("column is: $column\n" && (++$column)) && print("now column is:
-e syntax OK
More information about the spug-list