Conditional Statements: ifelse and unless

 < Day Day Up > 

Conditional Statements: if/else and unless

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

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.

NOTE

Truth might be relative, but in 1844, the English mathematician George Boole introduced a new branch of mathematics known as linguistic algebra. The discipline continues to be used today in computing science as Boolean logic. His concepts of AND, NOT, and OR enable you to compare values that are in one of two mutually exclusive states and then make a decision based on the result of the comparison. Programmers refer to the states as off/on, zero/one, true/false, and so on. This logic is used to interpret 0s and 1s and is at the very foundation of computers. Perl implements Boolean logic in its conditional statements.


Truth is defined in Perl in a way that might be unfamiliar to you, so be careful. Everything 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 isn't 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 29.3 uses the if/else structure and shows conditional statements using the eq string comparison operator.

Listing 29.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"; } 

NOTE

Perl was designed as a natural language, and it acknowledges idiomatic expressions that correspond with spoken English. The if statement is one good example. For example, you can say the following:

 } 

Alternatively, you could write it as you would more likely say it:

 print "Hello Rich!\n" if $name eq "Rich"; 


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, as you did in the if example.

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

Although it isn't a rule, you should try to put the more important part of the statement (think of it as a sentence) on the left so that it is easier to read.


     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net