Выбрать главу

Except for the comma operator and conditional expression, these operators can also be used with the assignment operator, similar to the way addition (+) can be combined with assignment (=), giving +=.

Special String Constants

Perl supports string constants that have special meaning or cannot be entered from the keyboard. Table 25.6 shows most of the constants supported by Perl.

TABLE 25.6 Perl Special String Constants

Expression Meaning
\\ The means of including a backslash
\a The alert or bell character
\b Backspace
\c Control character (like holding the Ctrl key down and pressing the C character)
\e Escape
\f Formfeed
\n Newline
\r Carriage return
\t Tab
\xNN Indicates that NN is a hexadecimal number
\0NNN Indicates that NNN is an octal (base 8) number

Conditional Statements

Perl offers two conditional statements, if and unless, which function opposite one another. if enables you to execute a block of code only if certain conditions are met so that you can control the flow of logic through your program. Conversely, unless performs the statements when certain conditions are not met. The following sections explain and demonstrate how to use these conditional statements when writing scripts for Linux.

if/else

The syntax of the Perl if/else structure is as follows:

if (condition) {

 statement or block of code

} elsif (condition) {

 statement or block of code

} else {

 statement or block of code

}

condition can be a statement that returns a true or false value.

Truth is defined in Perl in a way that might be unfamiliar to you, so be careful. Every thing in Perl is true except 0 (the digit zero), "0" (the string containing the number 0), "" (the empty string), and an undefined value. Note that even the string "00" is a true value because it is not one of the four false cases.

The statement or block of code is executed if the test condition returns a true value. For example, Listing 25.3 uses the if/else structure and shows conditional statements using the eq string comparison operator.

LISTING 25.3 if/elsif/else

if ($favorite eq "chocolate") {

 print "I like chocolate too.\n";

} elsif ($favorite eq "spinach") {

 print "Oh, I don't like spinach.\n";

} else {

 print "Your favorite food is $favorite.\n";

}

unless

unless works just like if, only backward. unless performs a statement or block if a condition is false:

unless ($name eq "Rich") {

 print "Go away, you're not allowed in here!\n";

}

NOTE

You can restate the preceding example in more natural language like this:

print "Go away!\n" unless $name eq "Rich";

Looping

A loop is a way to repeat a program action multiple times. A very simple example is a countdown timer that performs a task (waiting for one second) 300 times before telling you that your egg is done boiling.

Looping constructs (also known as control structures) can be used to iterate a block of code as long as certain conditions apply, or while the code steps through (evaluates) a list of values, perhaps using that list as arguments. Perl has four looping constructs: for, foreach, while, and until.

for

The for construct performs a statement (block of code) for a set of conditions defined as follows:

for (start condition; end condition; increment function) {