• Aucun résultat trouvé

The Order of Operations

Dans le document Teach Yourself Perl 5 in 21 days (Page 144-147)

You can write this using the conditional operator if you use the comma operator, as follows:

$var1 == 47 ? (print("var1 is already 47\n"), $is_fortyseven = 1) : ($var1 = 47, print("var1 set to 47\n"), $is_fortyseven = 0);

As you can see, this is difficult to understand. The basic rules are as follows:

Use the conditional operator for very simple conditional statements.

Use if and else for everything else.

Conditional Operators on the Left Side of Assignments

In Perl 5, you can use the conditional operator on the left side of an assignment. This enables you to assign a value to either of two variables, depending on the result of a conditional expression.

$condvar == 43 ? $var1 : $var2 = 14;

This statement checks whether $condvar has the value 43. If it does, $var1 is assigned 14. If it doesn't, $var2 is assigned 14.

Normally, you won't want to use conditional operators in this way because your code will become difficult to follow. Although the following code is a little less efficient, it performs the same task in a way that is easier to understand:

$condvar == 43 ? $var1 = 14 : $var2 = 14;

The Order of Operations

Perl, like all programming languages, has a clearly defined set of rules that determine which operations are to be performed first in a particular expression. The following three concepts help explain these rules:

The concept of precedence

The concept of associativity

The ability to override precedence and associativity using parentheses

Precedence

In grade school, you learned that certain arithmetic operations always are performed before other ones. For example, multiplication and division always are performed before addition and subtraction.

4 + 5 * 3

Here, the multiplication is performed first, even though the addition is encountered first when the statement is read from left to right. Because multiplication always is performed first, it has higher precedence than addition.

Table 4.6 defines the precedence of the operators in Perl. The items at the top of the table have the highest precedence, and the items at the bottom have the lowest.

Table 4.6. Operator precedence.

Operator Operation Performed

++, -- Autoincrement and autodecrement -, ~, ! Operators with one operand

** Exponentiation

=~, !~ Pattern-matching operators

*, /, %, x Multiplication, division, remainder, repetition

+, -, . Addition, subtraction, concatenation

<<, >> Shifting operators -e, -r, etc. File-status operators

<, <=, >, >=, lt, le, gt, ge

Inequality-comparison operators

==, !=, <=>, eq, ne, cmp

Equality-comparison operators

& Bitwise AND

|, ^ Bitwise OR and XOR

&& Logical AND

|| Logical OR

.. List-range operator

? and : Conditional operator (together)

=, +=, -=, *=, Assignment operators and so on

, Comma operator

not Low-precedence logical NOT

and Low-precedence logical AND

or, xor Low-precedence logical OR and XOR

Using this table, you can determine the order of operations in complicated expressions. For example:

$result = 11 * 2 + 6 ** 2 << 2;

To determine the order of operations in this expression, start at the top of Table 4.6 and work down. The first operator you see is

**, which means that it is performed first, leaving

$result = 11 * 2 + 36 << 2;

The next operation you find in the table is the * operator. Performing the multiplication leaves the following:

$result = 22 + 36 << 2;

The + operator is next:

$result = 58 << 2;

Next up is the << operator:

$result = 232;

The = operator is last on the list and assigns 232 to $result.

You might have noticed that Table 4.6 contains some operators that you've not yet seen and which you'll learn about later:

The list-range operator, defined on Day 5

The file-status operators, defined on Day 6, "Reading from and Writing to Files"

The pattern-matching operators, =~ and !~, defined on Day 7, "Pattern Matching"

Associativity

The rules of operator precedence enable you to determine which operation to perform first when an expression contains different operators. But what should you do when an expression contains two or more operators that have the same precedence?

In some cases, it doesn't matter what order you perform the operations in. For example:

$result = 4 + 5 + 3;

Here, $result gets 12 no matter which addition is performed first. However, for some operations the order of evaluation matters.

$result = 2 ** 3 ** 2;

If you perform the leftmost exponentiation first, $result is assigned 8 ** 2, or 64. If you perform the rightmost exponentiation first, $result is assigned 2 ** 9, or 512.

Because the order of operations is sometimes important, Perl defines the order in which operations of the same precedence are to be performed. Operations that are performed right-to-left (with the rightmost operation performed first) are said to be right associative. Operations that are performed left-to-right (with the leftmost operation performed first) are left associative.

Table 4.7 lists the associativity for each of the Perl operators. The operators are sorted according to precedence (in the same order as Table 4.6).

Table 4.7. Operator associativity.

Operator Associativity

++, -- Not applicable

-, ~, ! Right-to-left

-e, -r, Not applicable and so on

<, <=, >, >=, lt, le, gt, ge Left-to-right

==, !=, <=>, eq, ne, cmp Left-to-right

& Left-to-right

|, ^ Left-to-right

&& Left-to-right

|| Left-to-right

.. Left-to-right

? and : Right-to-left

=, +=, -=, *=, Right-to-left

and so on

, Left-to-right

not Left-to-right

and Left-to-right

or, xor Left-to-right

From Table 4.7, you see that the exponentiation operator is right associative. This means that in the following:

$result = 2 ** 3 ** 2;

$result is assigned 512, because the rightmost ** operation is performed first.

Dans le document Teach Yourself Perl 5 in 21 days (Page 144-147)