Mathemetical operators in Perl aren't simply performed left to right - very early on your
Learning to Program in Perl course you'll learn that multiplications and divisions happen before additions and subtractions, and you'll learn that brackets (also know as terms or lists) happen even earlier. Commonly this scheme - which is shared with all the other languages we teach - is referred to as
BODMAS:
Brackets
Order (sometimes BIDMAS - "Indeces")
Multiplication
Division
Addition
Subtraction
Thus:
$demo = 2 + 3 * 4 + 5; # see 1. below
print "Results is $demo\n";
$demo = (2 + 3) * 4 + 5; # see 2. below
print "Results is $demo\n";
$demo = (2 + 3) * (4 + 5); # see 3. below
print "Results is $demo\n";
Will results in 19, 25, and 45, because ...
1. 3 * 4 is 12. 2 + 12 is 14. 14 + 5 is 19.
2. 2 + 3 is 5. 5 * 4 is 20. 20 + 5 is 25.
3. 2 + 3 is 5. 4 + 5 is 9. 5 * 9 is 45.
Full source of this example
[here].
Remember that addition and subtraction are actually done at the same time, as are multiplication and division, so "BODMAS" is a bit of a cheat. And also remember that multiplication and division are done from left to right, thus
$bx = 24 / 2 * 3;
gives a result of 36
but
$bx = 24 / (2 * 3);
gives a result of 4, as does
$bx = 24 / 2 / 3;
The majority of operations are performed left to right, but some are run right to left instead, for example
=. That's how
$g = $h = 16;
can work - 16 is assigned to $h and the result (which is a copy of the value assigned) is then assigned to $g.
Finally, for some operations the left -> right or right -> left thing doesn't make any difference, as they can't be chained - the result is of a different type. These are "nonassociative" operations.
Here's a full table of the Perl operators, listed from highest precedence (performed first) to lowest precendence, with a note of which direction they're performed in where applicable.
| Operator | | Direction |
| terms and list operators (leftward) | | left |
| -> | | left |
| ++ -- | | n/a |
| ** | | right |
| ! ~ \ and unary + and - | | right |
| =~ !~ | | left |
| * / % x | | left |
| + - . | | left |
| << >> | | left |
| named unary operators | | n/a |
| < > <= >= lt gt le ge | | n/a |
| == != <=> eq ne cmp ~~ | | n/a |
| & | | left |
| | ^ | | left |
| && | | left |
| || // | | left |
| .. ... | | n/a |
| ?: | | right |
| = += -= *= etc. | | right |
| , => | | left |
| list operators (rightward) | | n/a |
| not | | right |
| and | | left |
| or xor | | left |
If in doubt ... add some brackets!
If it gets too complicated in a single statement, consider splitting it for maintainability (written 2011-12-07, updated 2011-12-12)
30ec
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P202 - Perl Fundamentals [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3329] Perl from basics - (2011-06-20)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1726] Hot Courses - Perl - (2008-07-28)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[184] MTBF of coffee machines - (2005-01-20)
Q101 - Object Orientation and General technical topics - Programming Principles [4090] Test Driven Development in Python - Customer Comes First - (2013-05-16)
[4061] Seamless, integrated IT - we have a long way to go! - (2013-04-11)
[4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
[3954] Lesson 1 in programing - write clean, reuseable and maintainable tidy code - (2012-12-16)
[3928] Storing your intermediate data - what format should you you choose? - (2012-11-20)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2769] Easy - but for whom? - (2010-05-18)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2586] And and Or illustrated by locks - (2010-01-17)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2510] The music of the stock market - (2009-11-22)
[2415] Variable names like i and j - why? - (2009-09-22)
[2327] Planning! - (2009-08-08)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[2228] Where do I start when writing a program? - (2009-06-11)
[2022] Pre and post increment - the ++ operator - (2009-02-03)
[2001] I have not programmed before, and need to learn - (2009-01-19)
Some other Articles
The difference between dot (a.k.a. full stop, period) and comma in PerlFinding all matches to a pattern in Perl regular expressionsLooking for hotel rooms in Melksham over Christmas? We still have some availabilitySome different pictures from MelkshamWhat order are operations performed in, in a Perl expression?I loves Melksham Easy session example in PHP - keeping each customers data apartSeparating program and artwork in PHP - easier maintainance, and better for the userSanta Train - another successful trip - 4th December 2011Letting the foster parents know ... too little or too much?