When you run this program there are no warnings:p2@nereida:~/src/perl/testing$ cat precedence1.pl my $n1 = 2; my $n2 = 4; print "$n1 plus $n2 is " . $n1 + $n2 . "\n";
However:pp2@nereida:~/src/perl/testing$ perl precedence1.pl 6
Issues the warning:pp2@nereida:~/src/perl/testing$ cat precedence2.pl use warnings; my $n1 =2; my $n2 = 4; print "$n1 plus $n2 is " . $n1 + $n2 . "\n";
The warning flag and the Warnings module are full of heuristics that helps you most of the time.pp2@nereida:~/src/perl/testing$ perl precedence2.pl Argument "2 plus 4 is 2" isn't numeric in addition (+) at precedence2. +pl line 6. 6
Whenever you have problems with operator priorities use the p option of Deparse like this:
It will tell you the execution order of the involved operations.pp2@nereida:~/src/perl/testing$ perl -MO=Deparse,-p precedence2.pl use warnings; use strict 'refs'; (my $n1 = 2); (my $n2 = 4); print(((("$n1 plus $n2 is " . $n1) + $n2) . "\n")); precedence2.pl syntax OK
Observe that the extraordinary thing is that the operation
still gives 6!. This is because the phrase starts with 4 plus .... There is some irony here.(((("$n1 plus $n2 is " . $n1) + $n2) . "\n"))
The first time I stomped in this sort of "Perl priority problems" I was puzzled. But if you live with it time enough you start to appreciate the fun of it
Hope it helps
Casiano
In reply to Re: This is odd.
by casiano
in thread This is odd.
by JDelmoso
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |