Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Web Programming Desktop Reference 6in1

Chapter 6 -- Understanding the Perl Reference Section

Chapter 6

Understanding the Perl Reference Section


CONTENTS


This section is a reference guide for the programming language called Perl. This book does not describe how to install Perl on your computer; if you do not already have Perl installed, it will not be very useful!


Note
If you want to install Perl and have access to the Internet visit the Central Perl Archive Network (CPAN). The master site is at ftp://ftp.funet.fi/pub/languages/perl/CPAN/, and there are many mirror sites around the world. This is as much as can be found on Perl installation in this guide!

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

This chapter describes the advantages of using Perl and outlines the structure of this section.

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 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 not worry about the details of what's happening in that code example until Chapter 7, "Perl Overview." Just notice two things:

In a nutshell, that's the secret to rapid development: Write small amounts of powerful code without having to pause to consider awkward issues of syntax at every step.

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:

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 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 problem categories.

It could have been expanded to handle these tasks by adding more and more keywords and operators, hence by 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 its 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 enable 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 losing 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 s