print "hello world\n"; # say hello world, followed by a newline
print "The answer is ";
print 6 * 7;
print ".\n";
You can actually give print a series of values,
separated by commas.
print "The answer is ", 6 * 7, ".\n";
This is actually a list, but we haven't
talked about lists yet, so we'll put that off for later.
2.6.2. Operator Precedence and Associativity
Operator
precedence determines which operations in a complex group of
operations happen first. For example, in the expression
2+3*4, do we perform the addition first or the
multiplication first? If we did the addition first, we'd get
5*4, or 20. But if we did the
multiplication first (as we were taught in math class), we'd
get 2+12, or 14. Fortunately,
Perl chooses the common mathematical definition, performing the
multiplication first. Because of this, we say multiplication has a
higher precedence than addition.
You can override the default precedence order by using
parentheses.
Anything in parentheses is completely computed before the operator
outside of the parentheses is applied (just like you learned in math
class). So if I really want the addition before the multiplication, I
can say (2+3)*4, yielding 20.
Also, if I wanted to demonstrate that multiplication is performed
before addition, I could add a decorative but unnecessary set of
parentheses, as in 2+(3*4).
While precedence is simple for addition and multiplication, we start
running into problems when faced with, say, string concatenation
compared with exponentiation. The proper way to resolve this is to
consult the official, accept-no-substitutes Perl operator precedence
chart, shown in Table 2-1.[60] (Note that some of the operators have not yet been
described, and in fact, may not even appear anywhere in this book,
but don't let that scare you from reading about them in the
perlop manpage.)
Table 2-2. Associativity and precedence of operators (highest to lowest)
Associativity
|
Operators
|
left
|
parentheses and arguments to list operators
|
left
|
->
|
|
++ -- (autoincrement and
autodecrement)
|
right
|
**
|
right
|
\ ! ~
+ - (unary operators)
|
left
|
=~ !~
|
left
|
* / %
x
|
left
|
+ - .
(binary operators)
|
left
|
<< >>
|
|
named unary operators (-X filetests,
rand)
|
|
< <=
> >=
lt le gt
ge (the "unequal" ones)
|
|
== !=
<=> eq
ne cmp (the "equal"
ones)
|
left
|
&
|
left
|
| ^
|
left
|
&&
|
left
|
||
|
|
.. ...
|
right
|
?: (ternary)
|
right
|
= += -=
.= (and similar assignment operators)
|
left
|
, =>
|
|
list operators (rightward)
|
right
|
not
|
left
|
and
|
left
|
or xor
|
In the chart, any given operator has higher precedence than all of
the operators listed below it, and lower precedence than all of the
operators listed above it. Operators at the same precedence level
resolve according to rules of associativity
instead.
Just like precedence, associativity resolves the order of operations
when two operators of the same precedence compete for three operands:
4 ** 3 ** 2 # 4 ** (3 ** 2), or 4 ** 9 (right associative)
72 / 12 / 3 # (72 / 12) / 3, or 6/3, or 2 (left associative)
36 / 6 * 3 # (36/6)*3, or 18
In the first case, the ** operator has right
associativity, so the parentheses are implied on the right.
Comparatively, the * and /
operators have left associativity, yielding a set of implied
parentheses on the left.
So should you just memorize the precedence chart? No! Nobody actually
does that. Instead, just use parentheses when you don't
remember the order of operations, or when you're too busy to
look in the chart. After all, if you can't remember it without
the parentheses, your maintenance programmer is going to have
the same trouble. So be nice to your maintenance
programmer.