A problem with precedence

M

Mark Hobley

The following statement gives a result of 10, which is what I expect, because
multiplication has a higher precedence than addition:

print 4 + 2 * 3; # 10

I now add brackets to change the precedence:

print (4 + 2) * 3; # This unexpectedly gives 6

Reversing the calculation:

print 3 * (4 + 2); # This gives 18, as expected

Why do I get the answer 6 to (4 + 2) * 3?

--

Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
L

Lukas Mai

Mark Hobley said:
I now add brackets to change the precedence:

print (4 + 2) * 3; # This unexpectedly gives 6

Use warnings.

$ perl -wle 'print (4 + 2) * 3;'
Useless use of multiplication (*) in void context at -e line 1.
6

$ perl -MO=Deparse -e 'print (4 + 2) * 3;'
print(6) * 3;
-e syntax OK

perldoc perlfunc:

Any function in the list below may be used either with or without
parentheses around its arguments. (The syntax descriptions omit
the parentheses.) If you use the parentheses, the simple (but
occasionally surprising) rule is this: It looks like a function,
therefore it is a function, and precedence doesn't matter.
Otherwise it's a list operator or unary operator, and precedence
does matter. And whitespace between the function and left
parenthesis doesn't count--so you need to be careful sometimes:

print 1+2+4; # Prints 7.
print(1+2) + 4; # Prints 3.
print (1+2)+4; # Also prints 3!
print +(1+2)+4; # Prints 7.
print ((1+2)+4); # Prints 7.

HTH, Lukas
 
A

Andrzej Adam Filip

The following statement gives a result of 10, which is what I expect, because
multiplication has a higher precedence than addition:

print 4 + 2 * 3; # 10

I now add brackets to change the precedence:

print (4 + 2) * 3; # This unexpectedly gives 6

It is interpreted as
(print(4+2))*3
 
T

Tad McClellan

Mark Hobley said:
The following statement gives a result of 10, which is what I expect, because
multiplication has a higher precedence than addition:

print 4 + 2 * 3; # 10

I now add brackets to change the precedence:

print (4 + 2) * 3; # This unexpectedly gives 6


You should always enable warnings when developing Perl code you know.

(especially when the Perl code's behavior does not meet your expectations.)


Anyway,

perldoc -f print

Also be
careful not to follow the print keyword with a left parenthesis
unless you want the corresponding right parenthesis to termi-
nate the arguments to the print ...


So use parenthesis around the function arguments for at least
this one statement, even if you don't like to use parenthesis
around function arguments:

print((4 + 2) * 3);
 
L

Lukas Mai

Abigail said:
Lukas Mai ([email protected]) wrote on MMMMDCIV September MCMXCIII in
<URL:<> Mark Hobley <[email protected]> schrob:
<>
<> > I now add brackets to change the precedence:
<> >
<> > print (4 + 2) * 3; # This unexpectedly gives 6
<>
<> Use warnings.

Actually, the fact this gives a warning is a damn good reason to NOT
use warnings.

[detailed explanation why "print (...) interpreted as function" is
broken]
<> $ perl -wle 'print (4 + 2) * 3;'
<> Useless use of multiplication (*) in void context at -e line 1.
<> 6
<>

Don't use warnings. Unless you rip out this stupid warning from your
copy of Perl. And I'm not joking.

Heh, I even wondered why I didn't get another warning for my code. On
second reading I see the two spaces. And yeah, I forgot to disable that
warning when I installed 5.8.8; I used to have a patch for 5.8.6
somewhere.

Lukas
 
R

robic0

Abigail said:
Lukas Mai ([email protected]) wrote on MMMMDCIV September MCMXCIII in
<URL:<> Mark Hobley <[email protected]> schrob:
<>
<> > I now add brackets to change the precedence:
<> >
<> > print (4 + 2) * 3; # This unexpectedly gives 6
<>
<> Use warnings.

Actually, the fact this gives a warning is a damn good reason to NOT
use warnings.

[detailed explanation why "print (...) interpreted as function" is
broken]
<> $ perl -wle 'print (4 + 2) * 3;'
<> Useless use of multiplication (*) in void context at -e line 1.
<> 6
<>

Don't use warnings. Unless you rip out this stupid warning from your
copy of Perl. And I'm not joking.

Heh, I even wondered why I didn't get another warning for my code. On
second reading I see the two spaces. And yeah, I forgot to disable that
warning when I installed 5.8.8; I used to have a patch for 5.8.6
somewhere.

Lukas
Unless you know the limitations of warnings you will have a problem in the
future. Its a warning, not an error. Some here would purpend its an error.
Its not. Turn warning's off on production code. If you don't know the
limitation's of the language parser your using you should not be a paid
proffessional!!!!!!
 
C

Charles DeRykus

Abigail said:
Lukas Mai ([email protected]) wrote on MMMMDCIV September MCMXCIII in
<URL:<> Mark Hobley <[email protected]> schrob:
<>
<> > I now add brackets to change the precedence:
<> >
<> > print (4 + 2) * 3; # This unexpectedly gives 6
<>
<> Use warnings.

Actually, the fact this gives a warning is a damn good reason to NOT
use warnings.

Because Perl is more often wrong about this warning than it is right.
If I were to advocate against using Perl, I'd point out the warnings
it gives with 'print', and then rest my case.

print(4 + 2) * 3; # No warning.
print (4 + 2) * 3; # Warning.
print (4 + 2) * 3; # No warning (!)
print (4 + 2) * 3; # No warning.

Four mistakes, only one warning.


I agree but the void context warnings returned are generally useful:

$ perl -wle 'print(4 + 2) * 3;' # (print(6) * 3);
Useless use of multiplication (*) in void context at -e line 1.

$ perl -wle 'print(4 + 2) * 3;'
Useless use of multiplication (*) in void context at -e line 1.
6
$ perl -wle 'print (4 + 2) * 3; '
Useless use of multiplication (*) in void context at -e line 1.
6
$ perl -wle 'print (4 + 2) * 3;'
print (...) interpreted as function at -e line 1.
Useless use of multiplication (*) in void context at -e line 1.
6
$ perl -wle 'print (4 + 2) * 3;'
Useless use of multiplication (*) in void context at -e line 1.
But it gets better.

print (4 + 2) * 3; # Warning.
printf (4 + 2) * 3; # Warning.
sprintf (4 + 2) * 3; # No warning.

5.8.7 does warn here though since the expression gets optimized away:

$ perl -wle 'sprintf (4 + 2) * 3;'
Useless use of a constant in void context at -e line 1.
sort (4 + 2) * 3; # Warning.

$ perl -wle 'sort (4 + 2) * 3;'
sort (...) interpreted as function at -e line 1.
Useless use of sort in scalar context at -e line 1.
Useless use of multiplication (*) in void context at -e line 1.
Use of uninitialized value in multiplication (*) at -e line 1.
sin (4 + 2) * 3; # No warning.

$ perl -wle 'sin (4 + 2) * 3;'
Useless use of a constant in void context at -e line 1.
 
X

xhoster

Abigail said:
Lukas Mai ([email protected]) wrote on MMMMDCIV September MCMXCIII in
<URL:<> Mark Hobley <[email protected]> schrob:
<>
<> > I now add brackets to change the precedence:
<> >
<> > print (4 + 2) * 3; # This unexpectedly gives 6
<>
<> Use warnings.

Actually, the fact this gives a warning is a damn good reason to NOT
use warnings.

Because Perl is more often wrong about this warning than it is right.

That is not my experience.

If I were to advocate against using Perl, I'd point out the warnings
it gives with 'print', and then rest my case.

print(4 + 2) * 3; # No warning.
print (4 + 2) * 3; # Warning.
print (4 + 2) * 3; # No warning (!)
print (4 + 2) * 3; # No warning.

Four mistakes, only one warning.

Strange, I get 6 warning.

print (...) interpreted as function at foo line 2.
print (...) interpreted as function at foo line 3.
Useless use of multiplication (*) in void context at foo line 1.
Useless use of multiplication (*) in void context at foo line 2.
Useless use of multiplication (*) in void context at foo line 3.
Useless use of multiplication (*) in void context at foo line 4.


Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top