Why Use Perl?

Perl has many uses, especially in UNIX system administrative tasks, which is where Perl was born and grew up. The name stands for "Practical Extraction and Report Language." Nowadays, Perl is seen by many as the ideal development language for Web server scripts.

Why Use Perl?

People use Perl because it is quick, efficient, and easy to maintain when programming a wide range of tasks, in particular those involving the manipulation of text files. Also, there are many others also using Perl who are prepared to share their code.

Rapid Development

Many programming projects are high level rather than low level. That means that they tend not to involve bit-level manipulations, direct operating system calls. Instead, they focus on reading from files, reformatting the output, and writing it to standard output-for example, a Web browser. With Perl, the programmer does not need to get involved in the details of how file handles and buffers are manipulated, how memory is allocated, and so on. You can tell it to slurp in the contents of a file and display it on the standard output device but with all newlines replaced by tabs:

while ( <INFILE> )  {  s/\n/\t/;  print;  }
Let's review the code. Just notice two things:
  • It's very short.
  • It's almost legible even without knowing any Perl, especially if you are familiar with C.

In a nutshell, quick code,no awkward syntax.

Perl is pithy; a little Perl code goes a long way. In terms of programming languages, that usually means that the code will be difficult to read and painful to write. But although Larry Wall, the author of Perl, says that Perl is functional rather than elegant, most programmers quickly find that Perl code is very readable and that it is not difficult to become fluent at writing it. This is especially true of the high-level, macro operations typically required in Web development.

As it happens, Perl is quite capable of handling some pretty low-level operations, too. It can handle operating system signals and talk to network sockets, for example.

Compiler and Interpreter

A program by itself can't achieve anything. To carry out its work, it needs to be fed to either a compiler or an interpreter. Both have their advantages:

  • A compiler takes a program listing and generates an executable file. This executable file can then be executed as many times as necessary, copied to other computers, and so on without the need for the program source code. This helps to keep program details confidential.
  • Because the compiler only runs once, it can afford to take its time about generating the executable code. As a result, compilers tend to perform elaborate optimization on the program code with the result that the executable code runs very efficiently.
  • An interpreter examines a program listing line by line and carries out the tasks required by the code there and then. There is no need for a separate compilation stage; once the program has been written, it can be executed without delay. This makes for a rapid development cycle.

There are advantages and disadvantages to both approaches. Compiled code takes longer to prepare, but then it runs fast and your source stays secret. Interpreted code gets up and running quickly but isn't as fast as interpreted code. You also need to distribute the program source code if you want to allow others to run your programs.

So which of these categories describes Perl?

Well, Perl is a little special in this regard; it is a compiler that thinks it's an interpreter. Perl compiles program code into executable code before running it, so there is an optimization stage and the executable code runs quickly. However, it doesn't write this code to a separate executable file. Instead, it stores it in memory and then executes it.

This means that Perl combines the rapid development cycle of an interpreted language with the efficient execution of compiled code. The corresponding disadvantages are also there, though: The need to compile the program each time it runs means a slower startup than a purely compiled language and requires developers to distribute source code to users.

In practice, these disadvantages are not too limiting. The compilation phase is extremely fast, so you're unlikely to notice much of a lag between invoking a Perl script and the start of execution.

In summary, Perl is compiled "behind the scenes" for rapid execution, but you can treat it as if it is interpreted. This makes it easy for you to tweak your HTML; just edit the code and let the users run it. But is that good programming practice? Hey, that's one for the philosophers.

Flexibility

Perl was not designed in the abstract. It was written to solve a particular problem and it evolved to serve an ever widening set of real-world problems.

It could have been expanded to handle these tasks by adding more and more keywords and operators, hence making the language bigger. Instead, the core of the Perl language started out small and became more refined as time went on. In some ways, it actually contracted; the number of reserved words in Perl 5 is actually less than half the number in Perl 4, not more.

This reflects an awareness that Perl's power lies in it's unique combination of efficiency and flexibility. Perl itself has grown slowly and thoughtfully, usually in ways that allow for enhancements and extensions to be added on rather than being hard-wired in. This approach has been critical in the development of Perl's extensibility over time, as the next section explains.

Extensibility

Much of the growth in Perl as a platform has come by way of the increasing use of libraries (Perl 4) and modules (Perl 5). These are mechanisms that allow developers to write self-contained portions of Perl code that can be slotted in to a Perl application.

These add-ons range from fairly high-level utilities, such as a module that adds HTML tags to text, to low-level, down-and-dirty development tools such as code profilers and debuggers.

The ability to use extensions like these is a remarkable advance in the development of a fairly slick language, and it has helped to fuel the growth in Perl use. It makes it easy for Perl developers to share their work with others; the arrival of objects in Perl 5 makes structured design methodologies possible for Perl applications. The language has come of age without loosing any of its flexibility or raw power.

Web Server Scripts

Web servers generate huge amounts of HTML. The "M" stands for "Markup," and you need lots of it to make your Web pages look more exciting than the average insurance contract. It's an awkward business though, with problems arising easily if tags are misplaced or misspelled. Perl is a good choice of language to look after the details for you while you get on with the big picture. This is especially true if you call on Perl 5's object-oriented capabilities.

Another facet of Perl that is of particular interest to many Web server managers is that Perl works very well with standard UNIX DBM files and support for proprietary databases is growing. This is a significant consideration if you plan to allow users to query database material over the Web.

Security

Security is a major issue when writing system administrative programs and on the Internet in general. Using Perl for scripting on your Web server, you can easily guard against users trying to sneak commands through for the server to execute on their behalf. There is also an excellent Perl 5 module called pgpperl, also known as Penguin, that allows your server to use public-key cryptography techniques to guard sensitive data from eavesdroppers.

Ubiquity

Lots of people on the Web already use Perl! Going with the flow isn't always the best approach, but Perl has grown with the Web. There is a lot of experience out there if you need advice. The Perl developers are keenly aware of Web issues as they add to Perl. And many Perl modules have been built with the Web specially in mind.

Why You Want to Use Perl

There are many reasons why you want to use Perl. It is small, efficient, flexible, and robust. Perl is particularly well suited for Web development work where text output is a major preoccupation. If the reasons previously outlined aren't quite enough, consider this: Perl is completely free.