Perl

Perl is a twenty-year-old scripting language designed for managing text. It is cross-platform, running on Linux, Unix, Mac OS X, Windows, and probably many more operating systems. It comes pre-installed on most operating systems today, and is used for managing server tasks, formatting documents, and filtering data. It may be the most-used programming language on the web, and has sometimes been called the duct-tape of the Internet.

If you’re familiar with the use of duct tape, you’ll have some idea of what Perl gets used for. Perl is not the prettiest of solutions. But it works. It holds together things that would otherwise never hold together, and it is a useful tool for creating quick solutions to thorny problems. There is an elegance in duct tape, an elegance in the solutions of the trenches. When something is broken it needs to be fixed.

Why use Perl?

If you’re a web page designer and you’re interested in programming, for example, you’re probably already using PHP. What makes Perl useful instead of PHP? The answer is the command line. Perl excels as filter and as glue. It is great at taking some input--usually text--and modifying it. It acts as a great text sausage machine, grinding up text and spitting it out.

Perl also makes for a great glue tying together the various command line programs you use and automating your use of them. Perl scripts are often used as cron jobs, running automatically at specified times. Perl is a great way of taking what you want to give your command line program and converting it into what the command line program expects. It is great at mediating between two or more data sources.

If you manage a web site or a MySQL database and need regular backups and monitoring, or if you need to regularly collect and collate data from a set of files, Perl is a great tool to know. If your task is a series of changes--if you can think of it as a series of sieves or as an assembly line of tasks--Perl can provide rapid automation for that task.

What do you need?

Sample Data

Go to http://www.hoboes.com/Mimsy/downloads/sampledata.zip for some sample text files. We’ll be using songs.txt in this tutorial.

Text Editor

You will need a text editor, such as Smultron on the Macintosh or NoteTabPro on Windows. If you intend to edit Perl scripts directly on a remote server, you will need familiarity with a Unix text editor such as vi or pico.

If you’re using a GUI text editor, you’ll want to make sure that it saves your files with Unix line endings. It usually won’t matter, but it can sometimes help track down errors.

Terminal or Shell

You will need to be able to execute your scripts. Normally you will do this from some sort of terminal or shell application. If you are running these scripts on a remote server, you will probably use ssh, or secure shell to get to that server. If you are running them on your local Mac OS X workstation, you’ll use the Terminal application in your Utilities folder.