Perl - the Programming Language - working draft

0 Conversations

Perl is a programming language. It has many uses, but a few examples are:

  • dynamic web1 pages;
  • "quick and dirty" programming of administrative tasks;
  • full-blown platform-independent graphical applications.

Perl started life as a scripting language under Unix. That origin shaped the language in some significant ways. In a scripting language, you want to just type code and run it, so Perl is interpreted rather than compiled2. You don't want to worry about declaring variables, so Perl doesn't require that. Admin tasks tend to call for text manipulation, so Perl excels in that.

Possibly the most striking feature of Perl is its users' strong community spirit, perhaps more than in any other language. They share code via CPAN and some of the most respected authorities in the language will answer your questions in Perl Monks.

Many Perl users are hackers3. Hackers program for fun, and for the intellectual challenge. Perl hackery includes obfuscation, Perl golf and JAPHs, all of which are discussed below.

The language

Like most modern programming languages, Perl builds on the experience of earlier languages. About Perl lists C, awk, sed, sh, and BASIC as major influences. As its creator, Larry Wall, says, Perl came out of his "laziness, impatience and hubris". His intention was to make "easy things easy and hard things possible". In the same interview, he explains the origin of the name. He "wanted a short name with positive connotations". Both "Practical Extraction and Report Language" and "Pathologically Eclectic Rubbish Lister" are backronyms.

That Perl caught on at all is evidence that Larry4 created a language that was better than its predecessors, at least some of the time. That other languages, notably Ruby, have emerged since suggests Perl isn't perfect.

A simple Perl program can be typed at the command line, for example, the traditional "Hello, world":

$ perl -e 'print "Hello, world!\n";'5

If you want to run a program more than once, it is convenient to store it in a file. The above becomes:

#! /usr/bin/perl

print "Hello, world!\n";

For anything more than a trivial example, this can save a great deal of typing. The first line in this file tells the operating system where to find the Perl interpreter.6

A common Perl slogan is "There's More Than One Way To Do It" (TMTOWTDI). Perl's versatility means that users coming to it from different languages can often find a familiar Perl idiom. It does contribute towards Perl's reputation for being difficult to read, and contrasts with, for example, Python's much more minimalist philosophy.

A common feature of many Perl programs is the use of regular expressions. These allow a Perl program to search through a string for substrings that match a specific pattern, and optionally replace those substrings. Regexes are not exclusive to Perl but Perl is often used in applications where they are useful.

Uses

Web

If you access a Web site that is more than a collection of static pages, if it has a login facility, if pages change in response to user inputs, then there is some software running on the server to make that happen. There's a good chance that software is written in Perl. Traditionally C and C++ (for example, dna, which drives h2g2) were popular alternatives to Perl; more recently Java, VBScript and Ruby have emerged. This list is not exhaustive.

Traditionally, dynamic Web pages were served using the Common Gateway Interface (CGI) protocol. The user's browser would request a page from a web server, which would start up a CGI program (written in Perl or whatever), which would generate the page and pass it back to the web server, which would return it to the user. This means that a separate instance of the CGI program would be generated for every page request. A much more efficient way of doing things is to build the Perl interpreter into the web server itself. mod_perl is an implementation of Perl that can be incorporated into the popular Apache web server. A third option, Embperl, allows Perl to be embedded within an HTML document, in much the same way as PHP.

CPAN

You can get some idea of the range of applications to which Perl has been put by browsing the Comprehensive Perl Archive Network (CPAN). Although CPAN includes complete programs (and Perl itself), the real wealth is in the variety of modules that you can incorporate into your own programs. Just a few examples are:

Perl Monks

Many Perl programmers subscribe to Perl Monks, an on-line community powered by the Everything engine (written in Perl, of course).

Perl Monks allows people (including non-members) to post entries (known as "nodes") in any of several "sections". The site is moderated by an experience system. Members can vote for or against nodes. Members gain "eXperience Points" (XP) and move up through a series of levels, mainly by posting nodes that attract large numbers of "upvotes".

Levels in Perl Monks are given whimsically ecclesiastical names: 1 = initiate, 2 = novice, etc. With increasing level, monks gain in their daily allowance of votes and, from level 9 = friar, can become involved in moderation, including "considering" nodes for "reaping" or removal. Moderation by voting and XP appears to work very well and reaping is rarely necessary.

Many Perl Monks give generously of their time in support of others, and it is perhaps invidious to name names. But the list of "Saints" who lead the XP rankings is headed by vroom (Tim Vroom, who created Perl Monks) followed by Merlyn (Randal Schwartz, co-author of Learning Perl). Larry Wall himself goes by the name of TimToady.

Just for fun

Obfuscation

Obfuscation, according to AskOxford, is to "make unclear or unintelligible". TMTOWTDI, together with Perl's relatively terse syntax, means that Perl programmers can write fiendishly obfuscated code, and other Perl programmers enjoy the challenge of unravelling these "obfu"s. Perl Monks has a whole section devoted to obfuscated code. One master of the obfu is Erudil. His camel code obfu, based on the cover of Programming Perl is a fine example.

CPAN

CPAN contains a great many modules that have been created mainly for the intellectual challenge. TheDamian (Damian Conway, author of Perl Best Practices) is responsible for some of the most extraordinary examples. His Lingua::Romana::Perligata allows the user to write Perl that looks uncannily like Latin, not only in the words but in the syntax. And his Acme::Bleach allows the user to write Perl that looks uncannily like nothing at all!

Perl golf

Perl golf, as the name (perhaps) suggests, is writing code to meet a specified requirement in as few (key-)strokes as possible. Frequent competitions are organised.

JAPHs

A JAPH is a perl program that generates the phrase "Just another Perl hacker,". The earliest JAPHs were by Randal Schwartz. JAPHs can be seen as a special case of obfuscation, sometimes combined with Perl golf as some Perl hackers (but not all) strive to fit their JAPHs within the conventional limit of four lines of 80 characters7 for email and Usenet signatures. "Abigail", a respected authority on the subject, has written a "presentation" on JAPHs8. As with all things Perlish, a variety of examples of JAPHs can be found at CPAN.

Further reading

  1. perlintro from the Perl documentation
  2. Perl, the first postmodern computer language by Larry Wall
1There are many webs (e.g. on intranets), but only one World Wide Web (on the Internet). Capitalisation reflects that distinction.2Machine code, the native language of computers, is almost unintelligible to programmers, so they (the programmers) have invented ways of translating "high-level languages" into machine code. These high-level languages are usually based on English, but occasionally other languages (including Klingon!)

When you write a program in Basic, Java, C++, Perl or whatever, it needs to be translated into machine code before it can be run. That translation is called interpretation or compilation, depending on when it is done. With an interpreter, the translation is done immediately before running, usually line-by-line. With a compiler, the translation is a separate step that results in an executable file (usually after an additional step to link in library functions).

Interpreters make it very easy to write code, because you can easily try a small change to your program and run it right away. But they make for relatively slow programs because the translation has to be done every time they are run. Compilers slow the development process because every time you change even a single line, you have to recompile the entire program before you can run it. The fact that the compiled program is saved in an executable (machine code) file means that it can run much faster than interpreted code. It also has the advantage that you can distribute your programs in a format that others cannot easily read or modify to their own purpose, hence preserving your intellectual property rights.



C++ is compiled, Perl is interpreted. Visual Basic can be either. Java is curiously in between. It is compiled to an intermediate, machine-independent, "bytecode", which is then translated by a program called a "Java virtual machine" into the machine code for a specific processor.
3"Hacker" is a word that, like "salt", "sugar" and "acceleration", has a much more specific meaning in popular usage than among the specialists who use it every day - see Hackers and Crackers.4Presumptious, but common usage suggests Larry's that kind of guy.5This example is written for Unix. Windows users might need C:\perl -e "print \"Hello, world!\n\";"6This is true of Unix. On a Windows system, this line serves no purpose (but does no harm).7Authorities differ as to whether the limit is "no more than 80" or "fewer than 80".8h2g2 rules forbid linking to this as the URL "contains a word which other users may find offensive".

Bookmark on your Personal Space


Conversations About This Entry

There are no Conversations for this Entry

Entry

A17869422

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more