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

Quality code

There has been much debate since programming languages were invented as to what constitutes "good code". At the time of writing, object-oriented programming languages are commonly seen as encapsulating a natural, intuitive, and solid programming style, in which is easy to write readily understandable programs. Like the shell, Perl was invented by capable and confident system programmers, people who perhaps did not feel the need for their code to be easily read by others.

Perl is not object-oriented. Perl does not come with interactive development environments which will help you write code. Perl is not forgiving of mistakes - it allows the programmer full licence to do what she or he wants.

It is possible to write Perl in variety of obscure ways so that your code is extremely difficult to read. This can be taken to extremes, for instance in the form of JAPH scripts. See www.perl.com/CPAN/misc/japh for examples. The onus is on you, the programmer, to write Perl so that your code is "good code". What does this mean in practice?

Use comments

Your program should be well commented, and perhaps a quarter of the code should be comments. Help the reader to understand what you're doing, since the Perl code itself might not.

Use meaningful variable names

It is tempting to use single letter names, since it's quicker to type, and makes your programs smaller. This is false economy, and it's easy to forget what you were using variables for.

Check for error conditions

If you know that your program only requires one argument (say), make sure that your code does something sensible when presented with 0 arguments or with more than one.

Structured your programs in a modular fashion

Use modules and functions. We have only touched on them in this chapter, but when you have gained experience programming Perl, and are used to writing longer programs, this will be helpful. In the meantime, if a program has several distinct phases, then ensure that they are distinguished. This can be done easily by separating them with blank lines and commenting them. Indenting the bodies of loops is another simple technique which clarifies the structure of the code.

Use standard libraries if appropriate

Perl comes supplied with libraries. Many of the tasks you may wish to accomplish can be written easily using modules from those libraries. Don't re-invent the wheel!

Worked example

Write a Perl script, which takes one integer argument, and displays a message to inform the user whether the number is prime. Ensure your code is of good quality.
Solution:
Suppose the argument is n. We solve this problem by successively trying to divide numbers from 2 up to n/2 into n. If any of these divides exactly, then n is not prime.

# Program to calculate whether a number is prime

# Part 1: Check the input data
if ($#ARGV != 0) {
    printf "Exactly one argument required\n";
    exit 1;
}

if ($ARGV[0] <= 0) {
    printf "The argument must be positive\n";
    exit 1;
}

# Part 2: Perform the calculation

# Variable number is the integer we are testing
$number = $ARGV[0];
$isPrime = 0;

# loop through all possible divisors
for ($divisor=2; $divisor <= $number/2; $divisor++) {
    if ($number % $divisor == 0) {
        $isPrime = 1;
        }
    }

# Part 3: output the result
if ($isPrime == 1) {
    printf "%d is not prime\n", $number;
}
else {
    printf "%d is prime\n", $number;
}

Worked example

The task is identical to the previous worked example. The solution in this case ignores our advice on good coding. We hope that the point is made!
Solution:

$n=$ARGV[0];$i=0;
for ($j=2;$j<=$n/2;$j++){if($n%$j==0){$i=1;}}
if($i==1){printf "%d is not prime\n", $n;}
else {printf "%d is prime\n", $n;}

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck