• Aucun résultat trouvé

Forcing Precedence Using Parentheses

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

Perl enables you to force the order of evaluation of operations in expressions. To do this, use parentheses as follows:

$result = 4 * (5 + 3);

In this statement, 5 is added to 3 and then multiplied by 4, yielding 32.

You can use as many sets of parentheses as you like:

$result = 4 ** (5 % (8 - 6));

Here, the result is 4:

8 - 6 is performed, leaving 4 ** (5 % 2)

5 % 2 is performed, leaving 4 ** 1

4 ** 1 is 4

DO use parentheses whenever you aren't sure whether a particular operation is to be evaluated first. For example, I don't know many programmers who remember that addition operators are evaluated before shifts:

$result = 4 << 2 + 3;

And virtually no one remembers that && has higher precedence than ||:

if ($value == 0 || $value == 2 && $value2 == "hello") { print("my condition is true\n");

}

You can make life a lot easier for people who read your code if you use parentheses when the order of evaluation is not obvious. For example:

$result = 4 << (2 + 3);

if ($value == 0 || ($value == 2 && $value2 == "hello")) { print("my condition is true\n");

}

DO use multiple lines, extra spaces, and indentation to make complicated expressions easier to read. For example:

if ($value == 0 ||

($value == 2 && $value2 == "hello")) {

Here, it's obvious that there are two main conditions to be tested and that one of them contains a pair of subconditions.

DON'T leave out closing parentheses by mistake.

$result = 4 + (2 << ($value / 2); # error

This statement will be flagged as erroneous because you are missing a closing parenthesis.

A handy way of checking whether you have enough parentheses in complicated expressions is to use this simple trick:

Start at the left end of your expression.

Starting from 0, add 1 for every left parenthesis you see.

Subtract 1 for every closing parenthesis you see.

If your final result is 0, you've got enough opening and closing parentheses. (This doesn't guarantee that you've put the parentheses in the right places, but at least you now know that you have enough of them.)

Summary

Today you learned about the operators that Perl supports. Each operator requires one or more operands, which are the values on which the operator operates. A collection of operands and operators is known as an expression.

The operators you learned how to use are as follows:

The arithmetic operators +, -, *, /, %, **, and unary negation

The integer-comparison operators ==, !=, <, >, <=, >=, and <=>

The string-comparison operators eq, ne, lt, gt, le, ge, and cmp

The logical operators ||, &&, and !

The bit-manipulation operators |, &, ^, ~, <<, and >>

The assignment operators =, +=, -=, *=, /=, %=, **=, !=, &=, ^=, and .=

The autoincrement operator ++

The autodecrement operator

--●

The string-concatenation operator .

The string-repetition operator x

The comma operator ,

The conditional operator (? and : together)

You also learned about operator precedence and associativity, two concepts that tell you which operators in an expression usually are performed first. Operator precedence and associativity can be controlled by putting parentheses around the operations you want to perform first.

Q&A

Q: Is there a limit on how large my expressions can be?

A: Effectively, no. There is a limit, but it's so large that no one would possibly want to create an expression that long, because it would be impossible to read or understand.***It's easier to understand expressions if they are shorter.

Q: Is it better to use += or ++ when adding 1 to a variable?

A: It's best to use ++ when using a variable as a counter in a while statement (or in other loops, which you learn about on Day 8, "More Control Structures"). For other addition operations, you should use +=.

Q: Why are some operators left associative and others right associative?

A: Most operators are left associative, because we normally read from left to right.

Assignment is right associative because it's easier to read. For instance:

$var1 = $var2 = 5;

If assignment happened to be left associative, $var1 would be assigned the old value of $var2, not 5. This would not be obvious to a casual reader of the program.Exponentiation is right associative because that's how exponentiation is

performed in mathematics.Other operators that are right associative are easier to read from right to left.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

Define the following terms:

a. operator

What operations are performed by the following operators?

a. &&

What operators perform the following operations?

a. string-equality comparison b. remainder

c. string duplication d. bitwise OR

e. numeric greater-than-or-equal-to 3.

What is the binary representation of the following numbers?

a. 171 b. 1105 c. 0 4.

What is the standard (base-10) representation of the following numbers?

a. 01100100 b. 00001111 c. 01000001 5.

What is the value of the following expressions?

a. 17 * 2 ** 3 / 9 % 2 << 2

Write a program that uses the << operator to print out the first 16 powers of 2.

1.

Rewrite the following statement using the conditional operator:

if ($var1 == 5 || $var2 == 7) {

Rewrite the following expression using the if and else statements:

$result = $var1 <= 26 ? ++$var2 : 0;

3.

Write a program that reads two integers from standard input (one at a time), divides the first one by the second one, and prints out the quotient (the result) and the remainder.

4.

Why might the following statement not assign the value 5.1 to $result?

$result = 5.1 + 100005.2 - 100005.2;

5.

Determine the order of operations in the following statement, and add parentheses to the statement to indicate this order:

6.

$result = $var1 * 2 << 5 + 3 || $var2 ** 3, $var3;

What value is assigned to $result by the following code?

$var1 = 43;

$var2 = 16;

$result = ++$var2 == 17 ? $var1++ * 2 - 5 : ++$var1 * 3 - 11;

7.

BUG BUSTER: Find and fix the bugs in the following program:

#!/usr/local/bin/perl

$num = <STDIN>;

chop ($num);

$x = "";

$x += "hello";

if ($x != "goodbye" | $x == "farewell") {

$result = $num eq 0 ? 43;

} else {

$result = ++$num++;

}

print("the result is $result\n");

8.

Chapter 7

Special Characters in Patterns The + Character

The [] Special Characters

The * and ? Special Characters

Escape Sequences for Special Characters

Matching Any Letter or Number

Anchoring Patterns

Variable Substitution in Patterns

Matching a Specified Number of Occurrences

Specifying Choices

Reusing Portions of Patterns

Pattern-Sequence Scalar Variables

Special-Character Precedence

Specifying a Different Pattern Delimiter

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