People plan their lives by making decisions, and so do programs. Figure 7.1 is a flow chart. A flow chart is defined as a pictorial representaion of how to plan the stages of a program. It helps you to visualize what decisions need to be made to accomplish a task. According to computer science books, a good language allows you to control the flow of your program in three ways:
Execute a sequence of statements.
Based on the results of a test, branch to an alternative sequence of statements.
Repeat a sequence of statements until some condition is met.
So far, we have seen script examples that are linear in structure; that is, simple statements that are executed in sequence, one after the other. Control structures, such as branching and looping statements, allow the flow of the program's control to change depending on some conditional expression.
The decision-making constructs (if, if/else, if/elsif/else, unless, etc.) contain a control expression that determines whether a block of statements will be executed.
The looping constructs (while, until, for, foreach) allow the program to repetitively execute a statement block until some condition is satisfied.
A compound statement, or block, consists of a group of statements surrounded by curly braces. The block is syntactically equivalent to a single statement and usually follows an if, else, while, or for construct. But unlike C, where curly braces are not always required, Perl requires them even with one statement when that statement comes after the if, else, while, etc. The conditional modifiers, discussed in Chapter 8, "Regular Expressions—Pattern Matching," can be used when a condition is evaluated within a single statement.
The if and unless constructs are followed by an expression surrounded by parentheses and followed by a block of one more statements. The block is always enclosed in curly braces.
An if statement is a conditional statement. It allows you to test an expression and, based on the results of the test, make a decision. The expression is enclosed in parentheses, and Perl evaluates the expression in a string context. If the string is non-null, the expression is true; if it is null, the expression is false. If the expression is a numeric value, it will be converted to a string and tested. If the expression is evaluated to be true (non-null), the next statement block is executed; if the condition is false (null), Perl will ignore the block associated with the expression and go on to the next executable statement within the script.
The unless statement is constructed exactly the same as the if statement; the results of the test are simply reversed. If the expression is evaluated to be false, the next statement block is executed; if the expression is evaluated to be true, Perl will ignore the block of statements controlled by the expression.
The if statement consists of the keyword if, followed by a conditional expression, followed by a block of one or more statements enclosed in curly braces. Each statement within the block is terminated with a semicolon (;). The block of statements collectively is often called a compound statement. Make sure when you are testing strings that you use the string operators shown in Table 6.7 and that if testing numbers, you use the numeric operators also shown in Table 6.6. Perl converts strings and numbers to conform to what the operator expects, so be careful. A test such as "yes" == "no" is incorrect. It should be "yes" eq "no".
Another form of the if statement is the if/else construct. This construct allows for a two-way decision. If the first conditional expression following the if keyword is true, the block of statements following the if are executed. Otherwise, if the conditional expression following the if keyword is false, control branches to the else, and the block of statements following the else are executed. The else statement is never an independent statement. It must follow an if statement. When the if statements are nested within other if statements, the else statement is associated with the closest previous if statement.
Formatif (Expression) {Block} else {Block} Example 7.2.
|
Yet another form of the if statement is the if/else/elsif construct. This construct provides a multiway decision structure. If the first conditional expression following the if keyword is true, the block of statements following the if is executed. Otherwise, the first elsif statement is tested. If the conditional expression following the first elsif is false, the next elsif is tested, etc. If all of the conditional expressions following the elsifs are false, the block after the else is executed; this is the default action.
Formatif (Expression1) {Block} elsif (Expression2) {Block} elsif (Expression3) {Block} else {Block} Example 7.3.
|
The unless statement is similar to the if statement, except that the control expression after the unless is tested for the reverse condition; that is, if the conditional expression following the unless is false, the statement block is executed.
The unless/else and unless/elsif behave in the same way as the if/else and if /elsif statements with the same reversed test as previously stated.
Formatunless (Expression) {Block} unless (Expression) {Block} else {Block} unless (Expression) {Block} elsif (Expression) {Block}... else {Block} Example 7.4.
|
Code View: (The Script) #!/bin/perl # Scriptname: excluder 1 while(<>){ 2 ($name, $phone)=split(/:/); 3 unless($name eq "barbara"){ $record="$name\t$phone"; 4 print "$record"; } } 5 print "\n$name has moved from this district.\n"; (Output) $ excluder names igor chevsky 408-123-4533 paco gutierrez 510-453-2776 ephram hardy 916-235-4455 james ikeda 415-449-0066 barbara kerz 207-398-6755 jose santiago 408-876-5899 tommy savage 408-876-1725 lizzy stachelin 415-555-1234 barbara has moved from this district. Explanation
|