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

Modules

A Perl module is a collection of Perl programs written to be used in other programs. You can write your own, or you can use modules written by other people. Some modules are included with each distribution of Perl, and others are made freely available on the Web. The web site www.cpan.org ("Comprehensive Perl Archive Network") is the main place to go to find out what is available. The site is continually growing, and at the time of writing contained over 1600 modules, including modules in the following areas:

  • security, encryption and authentication;
  • database support for most commercial databases;
  • web support, including HTML parsers and HTTP servers;
  • email clients and servers;
  • mathematics;
  • image manipulation;
  • archiving and file conversion;
  • interfaces to operating systems, including MacOS, OS2 and Windows;
  • hardware drivers; and
  • specialised computational modules, such as Neural Networks and Artificial Intelligence.

In order to use a module, include the line

use name-of-module;

at the start of your program.

Worked example

Write a Perl script to take two arguments, considered to be filenames, and copy the contents of the first file to the second.
Solution: Use the copy function in the File::Copy module:

use File::Copy;

copy($ARGV[0], $ARGV[1]) or die "Cannot perform copy"

Note that we have checked the successful completion of the copy function and generated an error message if it failed.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck