Main index

Introducing UNIX and Linux


Perl

Overview
Introduction
      Why yet another utility?
      Beginning Perl
      Invoking Perl
      Documentation on perl
      Perl Scripts
Variables
Input and output
      Files and redirection
      Pipes
      The DATA filehandle
Fields
Control structures
Predefined Perl
      Functions
      Modules
Regular expressions
      Single character translation
      String editing
Perl and the Kernel
Quality code
When do I use Perl?
Summary
Exercises

Control structures

The phrase control structures refers to the statements in the language that allow for choices and repetitions. In practice, this usually means if statements and loops.

The condition can be anything that evaluates to true or false. Similarly, a while statement will repeatedly execute until the condition ceases to be true:

while ( condition ) {
      code to execute while condition is true
   }

We have already met a while loop in the previous section, in the context of repeatedly reading in lines of input. In that case, the condition <STDIN> is assumed to be true if there is another line to read in, and false if the input has terminated. Let's look at a more typical use of while in an example:

Worked example

Read in two numbers from standard input, and prompt the user to enter their product. Continue to do so until the correct answer is entered.
Solution: Read in the two numbers, on separate lines of input, into variables $n1 and $n2, then prompt the user to multiply them together. Read in the user's answer into $answer, check whether this is different to $n1*$n2, and if so tell the user their answer is wrong, ask them to try again, and read in $answer again. Repeat until the user enters a correct answer.

# Prompt for input, and read two numbers
print "Type in the first number: ";
$n1 = <STDIN>;
print "Type in the second number: ";
$n2 = <STDIN>;

# Prompt for, and read, answer
print "Type in their product: ";
$answer = <STDIN>;

# Loop until correct answer input
while ($answer != $n1*$n2) {
  print "Wrong - try again: ";
  $answer = <STDIN>;
}
print "Correct - the answer is $answer\n";

Similary, at its simplest, an if statement takes the following form:

if ( condition ) {
      code to execute if condition true
   } else {
      code to execute if condition false
   }

There is also a variant of the syntax for if. Instead of

if ( condition ) { statement }

we can write

condition && { statement }

and instead of

if (! condition ) { statement }

we can write

condition || { statement }

The symbol && is pronounced and, and || is pronounced or. The words and and or can be used in place of && and || respectively. This is simply a way of making code easier to read. For instance, if you wished to open file myfile for reading, and wanted Perl to inform you if the file could not be read, the following one line of code could be used:

open(INPUT, "myfile") || die "Cannot open myfile";

The die command causes the message following die to be displayed on the standard error stream and the Perl program to terminate immediately with non-zero exit status.

Worked example

Your login shell reads in commands from the first of the following two files which may exist in your home directory: .bash_profile, .profile. Write a Perl script either to display the contents of that file, or to inform you that neither file exists.
Solution: Try to open each of the files in turn, and if both fail die. If one has been opened, display its contents.

# Try to open the one of the two files
open(INPUT, ".bash_profile") ||
  open(INPUT, ".profile") ||
  die "Neither .bash_profile nor .profile found";

# If one is open, use filehandle INPUT to read from it
while (<INPUT>) {
  print "$_";
}

# Finally, close the file
close(INPUT);

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck