-->
Previous Table of Contents Next


The each operator enables you to easily iterate through each element that is stored in an associative array. When you run the each operator on an associative array, it returns the first element of the array. Successive calls of each on the same array return the next element in the array until the last element is reached. The following code goes through the steps the coffee array used in the last example and displays the price of each kind of coffee stored in the array.


while (($type, $cost) = each(%coffee)) {

    print “The price of $type coffee is $cost\n”;

}

Perl Programming Constructs

Perl provides many of the same programming constructs that are found in all high-level programming languages. This section describes each of the programming constructs provided by Perl.

Statement Blocks

Perl statement blocks consist of one or more Perl statements enclosed in curly braces. The following code shows the sample syntax for a Perl statement block:


{

        statement1;

        statement2;

        …

}

Statement blocks can be used anywhere that a single Perl statement can be used. They are most commonly used to group statements that are to be executed as part of an iteration statement, such as a for or while statement or as part of a conditional statement, such as an if statement.

if Statements

Perl’s if statement is used to execute statement blocks conditionally. The syntax of the if statement is similar to the if statement syntax used in C. This syntax is shown by the following:


if (expression) {

        statements;

}

In this form, the if statement evaluates the expression, and if the result of that evaluation is True, it executes the statements contained in the statement block. If the expression evaluates to False, the statements in the statement block are not evaluated.

The if statement can be optionally accompanied by one or more else if clauses and one else clause. The syntax for a statement that contains two else if clauses and an else clause is shown in the following code:


if (expression1) {

        statements;

} elsif (expression2) (

        statements;

} elsif (expression3) {

        statements;

} else {

        statements;

}

In this form, the if statement evaluates expression1, and if the result of that evaluation is True, it executes the statements in the first statement block. If expression1 evaluates to False, expression2 is evaluated. If expression2 evaluates to True, the statements in the second statement block are executed. If expression2 evaluates to False, the process is repeated for the third expression. If expression3 evaluates to False, the statements in the last statement block are executed. The important thing to remember here is that one and only one of the statement blocks is executed.


Note:  
In Perl, an expression evaluates to True if it has a value other than zero and to False if it has a value of zero. This is different than utilities like test, which are the opposite. The reason for the difference is because Perl uses the C language’s syntax for consistency among programming languages.

The following example illustrates the use of an if statement:


print “Please enter your name or \”help\” for a \n”;

print “description of this program\n”;

$name=<STDIN>;

chop($name);

if ( $name eq “” ) {

        print “This program cannot proceed without your name.\n”;

        exit;

} elsif ( $name eq “help”) {

        print “This program reads in the user’s name and \n”;

        print “prints a welcome message on the screen if the\n”;

        print “name that was entered was not null.\n”;

} else {

        print “Hello $name, welcome to Perl.\n”;

}

This program asks for your name by displaying a prompt to the screen and waiting for you to enter your name on the command line. The third line of the program assigns whatever you type on the command line to the $name variable. The if statement then checks to make sure that you typed something when you were asked for your name. If the $name variable is empty, the program prints a statement that informs you that you must enter your name if you want to proceed. Next, the statement checks to see if you typed the name help on the command line. If you did, the program displays a description of itself to the screen. If the name you typed was not null and was not help, the program prints a welcome message.

A new command called chop is introduced in this example. The chop command removes the last character from the argument that is passed to it. You need to do this in the example because the last character of the entered name is a newline character. If you don’t remove the newline character, your last print command prints the following output if you, for example, type in the name John on the command line:


Hello John

, welcome to Perl.

unless Statements

The Perl unless statement is the opposite of the Perl if statement. It evaluates an expression, and if the expression returns a value of False, it executes its statement block. The syntax of the unless statement is


unless (expression) {

        statements;

}

Any statement you can write with an unless statement can also be written as an if statement. The only difference is that you have to negate the expression when you rewrite it as an if. For this reason, the unless statement is not used very often. The following code illustrates a possible use of an unless statement.


print “Please enter your account number.\n”

$account = <STDIN>

unless ($account < 1000 ) {

        print “Since you are a preferred customer you will\n”

        print “get a 10% discount\n”

}

This program asks for your account number, and if the account number is greater than or equal to 1000, it displays the message indicating that you are entitled to a 10% discount. If your account number is less than 1000, the program continues processing after the end of the unless statement.


Previous Table of Contents Next