The behavior is the correct one if you supress the warnings flag:
p2@nereida:~/src/perl/testing$ cat precedence1.pl my $n1 = 2; my $n2 = 4; print "$n1 plus $n2 is " . $n1 + $n2 . "\n";
When you run this program there are no warnings:
pp2@nereida:~/src/perl/testing$ perl precedence1.pl 6
However:
pp2@nereida:~/src/perl/testing$ cat precedence2.pl use warnings; my $n1 =2; my $n2 = 4; print "$n1 plus $n2 is " . $n1 + $n2 . "\n";
Issues the warning:
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
The warning flag and the Warnings module are full of heuristics that helps you most of the time.

Whenever you have problems with operator priorities use the p option of Deparse like this:

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
It will tell you the execution order of the involved operations.

Observe that the extraordinary thing is that the operation

(((("$n1 plus $n2 is " . $n1) + $n2) . "\n"))
still gives 6!. This is because the phrase starts with 4 plus .... There is some irony here.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.