Beginner’s Introduction to Perl

December 20, 2007

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.

In this first part of our series, you’ll learn a few basics about Perl and see a small sample program.

A Word About Operating Systems

In this series, I’m going to assume that you’re using a Unix system and that your Perl interpreter is located at /usr/local/bin/perl. It’s OK if you’re running Windows; most Perl code is platform-independent.

Your First Perl Program

Take the following text and put it into a file called first.pl:

#!/usr/local/bin/perlprint "Hi there!n";

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.

Functions and Statements

Perl has a rich library of functions. They’re the verbs of Perl, the commands that the interpreter runs. You can see a list of all the built-in functions on the perlfunc main page. Almost all functions can be given a list of parameters, which are separated by commas.

The print function is one of the most frequently used parts of Perl. You use it to display things on the screen or to send information to a file (which we’ll discuss in the next article). It takes a list of things to output as its parameters.

print "This is a single statement."; print "Look, ", "a ", "list!";

A Perl program consists of statements, each of which ends with a semicolon. Statements don’t need to be on separate lines; there may be multiple statements on one line or a single statement can be split across multiple lines.

print "This is "; print "two statements.\n"; print "But this ", "is only one statement.\n";

Numbers, Strings and Quotes

There are two basic data types in Perl: numbers and strings.

Numbers are easy; we’ve all dealt with them. The only thing you need to know is that you never insert commas or spaces into numbers in Perl. always write 10000, not 10,000 or 10 000.

Strings are a bit more complex. A string is a collection of characters in either single or double quotes.

The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted. For example, the character sequence \n is a newline character when it appears in a string with double quotes, but is literally the two characters, backslash and n, when it appears in single quotes.

print "This stringnshows up on two lines.";     print 'This string n shows up on only one.';

Variables

If functions are Perl’s verbs, then variables are its nouns. Perl has three types of variables: scalars, arrays and hashes. Think of them as “things,” “lists,” and “dictionaries.” In Perl, all variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores.

Scalars are single things. This might be a number or a string. The name of a scalar begins with a dollar sign, such as $i or $abacus. You assign a value to a scalar by telling Perl what it equals.

You don’t need to specify whether a scalar is a number or a string. It doesn’t matter, because when Perl needs to treat a scalar as a string, it does; when it needs to treat it as a number, it does. The conversion happens automatically. (This is different from many other languages, where strings and numbers are two separate data types.)

Numbers in Perl can be manipulated with the usual mathematical operations: addition, multiplication, division and subtraction. (Multiplication and division are indicated in Perl with the * and / symbols, by the way.)

$a = 5;     $b = $a + 10;       # $b is now equal to 15.     $c = $b * 10;       # $c is now equal to 150.     $a = $a - 1;        # $a is now 4, and algebra teachers are cringing.

Loops

Almost every time you write a program, you’ll need to use a loop. Loops allow you run a particular piece of code over and over again. This is part of a general concept in programming called flow control.

Perl has several different functions that are useful for flow control, the most basic of which is for. When you use the for function, you specify a variable that will be used for the loop index, and a list of values to loop over. Inside a pair of curly brackets, you put any code you want to run during the loop:

for $i (1, 2, 3, 4, 5) { print "$i\n"; }

Play Around!

Source http://www.perl.com/pub/a/2000/10/begperl1.html

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word