New Document
What is Perl?

Perl stands for "Practical Extraction and Report Language" or "Pathologically Eclectic Rubbish Lister." It is a powerful glue language useful for tying together the loose ends of computing life.

Perl is a programming language which can be used for a large variety of tasks. Both Perl interpreters and Perl documentation are freely available for Unix, MVS, VMS, MS/DOS, Macintosh, OS/2, Amiga, and other operating systems. (On Windows systems, Perl can be used in DOS command line mode.)

A typical simple use of Perl would be for extracting information from a text file and printing out a report or for converting a text file into another form. In fact, the name "Perl" was formed from the expression "Practical Extraction and Report Language". But Perl provides a large number of tools for quite complicated problems, including system administration tasks, which can often be programmed rather portably in Perl.

Perl has powerful string-manipulation functions. On the other hand, it eclectically combines features and purposes of the C language and many command or scripting languages. For such reasons, Perl looks rather odd on first sight. But once you have learned Perl, you will be able to write programs in Perl much faster than in most other languages. This makes Perl especially suitable for writing programs which are used once only.

The following simple Perl program reads a text file consisting people's names like "John Smith", each on a line of its own, and it prints them out so that the first and second name have been swapped and separated with a comma (e.g. "Smith, John").

while(<>) {
  split;
  print "$_[1], $_[0]\n"; }

  

As you can see, Perl is compact - and somewhat cryptic, until you learn some basic notations. (There will be a rather similar example, which is annotated in some detail.)

Perl has become popular for programming handlers for World Wide Web forms and generally as glue and gateway between systems, databases, and users.

Perl is typically implemented as an interpreted (not compiled) language. Thus, the execution of a Perl program tends to use more CPU time than a corresponding C program, for instance. On the other hand, computers tend to get faster and faster, and writing something in Perl instead of C tends to save your time.

Programs written in Perl are often called Perl scripts, especially in the context of CGI programming, whereas the term the perl program refers to the system program named perl for executing Perl scripts. (What, confused already?)

If you have used Unix shell scripts or awk or sed or similar utilities for various purposes, you will find that you can normally use Perl for those and many other purposes, and the code tends to be more compact. And if you haven't used such utilities but have started thinking you might have need for them, then perlhaps what you really need to learn is Perl instead of all kinds of futilities.

Perl is relatively stable; the current major version, Perl 5, was released in 1994. Extensions to the language capabilities are mainly added though the construction of modules, written by different people and made available to other Perl programmers. For major general-purpose Perl applications, particularly CGI scripts and client or server applications, see CPAN Module documentation.

The following is the "Sales Pitch" by Doug Sheppard.

Perl is the Swiss Army chainsaw of scripting languages: powerful and adaptable. It was first developed by Larry Wall, a linguist working as a systems administrator for NASA in the late 1980s, as a way to make report processing easier. Since then, it has moved into a large number of roles: automating system administration, acting as glue between different computer systems; and, of course, being one of the most popular languages for CGI programming on the Web.

Why did Perl become so popular when the Web came along? Two reasons: First, most of what is being done on the Web happens with text, and is best done with a language that's designed for text processing. More importantly, Perl was appreciably better than the alternatives at the time when people needed something to use. C is complex and can produce security problems (especially with untrusted data), Tcl can be awkward and Python didn't really have a foothold.

It also didn't hurt that Perl is a friendly language. It plays well with your personal programming style. The Perl slogan is ``There's more than one way to do it,'' and that lends itself well to large and small problems alike.

Creating, Storing and Running Perl Programs

The machines in the laboratories of the School of Computer Science have Perl installed under both UNIX and Windows-NT. For details, please consult the Technical Services Group's Website. To create a Perl program, you will need a text editor. In UNIX, you can use emacs, and in Windows-NT, you can use WordPad (you have to save the document in plain text format). In Windows-NT, you have to run your program in DOS-Shell.


Exercise


Take the following text (using either emacs or WordPad) and put it into a file called first.pl:

 #!/usr/local/bin/perl

     print "Hi there!\n";

  

(Traditionally, first programs are supposed to say Hello world!, but I'm an iconoclast.)


Now, run it with your Perl interpreter. From a command line, go to the directory with this file and type perl first.pl. You should see:

      Hi there!

  

The \n indicates the ``newline'' character; without it, Perl doesn't skip to a new line of text on its own.


Comments and Commands

After the header line: #!/usr/bin/perl there are either empty lines with no effect or commandlines or commentary lines. Everything from and behind a "#" up to the end of the line is comment and has no effect on the program. Commands start with the first non space charachter on a line and end with a ";". So one can continue a command over several lines and terminates it only with the semicolon.

History of Perl
Perl was first released December 1987. It was created by Larry Wall, then a systems administrator with JPL, to produce reports from multiple servers on a wide area network. Wall remains the "Benevolent Dictator" overseeing development of the language, which as it stands now represents the contribution of many, many people. Wall trained as a linguist before becoming a sysadmin, and that definitely had an affect on the development of the Perl language, particularly its tolerance of ambiguity and contextual interpretation.
Perl 5 was released in 1994. Perl 5 is still being actively maintained, is the most commonly used version of the language, and is what we will be looking at in this course. Perl 6 is in development, and has been for a long time. It is questionable as to whether version 6 will ever be as widely implemented as its predecessor.
In the early days of the web (mid-90s), Perl gained a lot of traction because web programming involves processing a lot of text (generally in the form of HTML). As specialized web-centric languages like PHP rose to prominence, Perl has become less important for web development, but it still has a place in that niche.
Uses of Perl
  • Tool for general system administration
  • Processing textual or numerical data
  • Database interconnectivity
  • Common Gateway Interface (CGI/Web) programming
  • Driving other programs! (FTP, Mail, WWW, OLE)
  • Why Perl?
    The question may arise as to why we are studying Perl rather than, say, C++ or Java. Languages are optimized for different things. Perl's roots in text processing & data manipulation make it highly relevant to library work, because a lot of what librarians work with is text. The availability of a number of library-related perl modules is another consideration, of which more in a moment.
    Also, Perl's C-like syntax has wide applicability for learning other languages. Anyone who knows Perl can pick up PHP, Python, Java more easily.
                                                                                                                                                           Next

    Back to Top