The Perl You Need to Know - Introduction to Perl

25
The Perl You Need to Know Aaron Weiss April 26, 1999 Perl - the very thought evokes a heady, adrenaline rush in the hearts of hard-core hackers, and an intimidating sense of anxiety in practically everyone else. The good news is that you don't need to master Perl to make it useful: Perl is simply a hammer, with which you can build a birdhouse or a mansion. And you don't need to be Bob Villa to build a birdhouse. In this article we begin a series on using Perl in web development scenarios. To start, we ease in with a general introduction to Perl itself. Future articles will take more in-depth looks at Internet capabilities you can leverage from within Perl programs. This article assumes Perl 5 as the reference version of Perl. Developers who have some familiarity with any other programming language, from BASIC to Pascal to C to JavaScript, will benefit the most from this article; however, readers with no programming background can still follow along and pick up most of the concepts. "A minute to learn, a lifetime to master" is the slogan for the board game Othello, but it might just as well belong to Perl. Put more accurately, the basic concepts behind Perl are simple to understand, especially for developers familiar with other programming languages. Yet, the open-ended flexibility of Perl offers seemingly endless possibilities -- which is where the exhilaration and/or intimidation usually sets in. The good news is that you don't need to master Perl to make it useful. Web developers take heart: Perl is simply a hammer, with which you can build a birdhouse or a mansion. And you don't need to be Bob Villa to build a\ birdhouse. In this article we begin a series on using Perl in web development scenarios. To start, we ease in with a general introduction to Perl itself. The purpose of this introduction is not to send you off writing enormous Perl programs -- there are plenty of articles and books which already succeed at this. Rather, this tutorial should arm you with a basic understanding of Perl so that you can work through the examples you care about -- web interaction -- without first needing to become a Perl guru. Future articles will take more in-depth looks at Internet capabilities you can leverage from within Perl programs. This article assumes Perl 5 as the reference version of Perl, which has supplanted the popular but older Perl 4 by several years now. Developers who have some familiarity with practically any other programming language, from BASIC to Pascal to C to JavaScript, will benefit the most from this article; however, readers with no programming background can still follow along and pick up most of the concepts by inference. Contents: Who's Afraid of Perl? Getting Perl Running Perl Programs Perl Variables: Scalars Table 1. Basic Perl Operators Perl Variables: Comparing Scalars Table 2. Perl Comparison Operators Perl Variables: Lists (aka Arrays) Perl Variables: Hashes Perl Functions Perl Program Flow: Conditionals and Loops Conclusion

description

The hidden secrets about PERL

Transcript of The Perl You Need to Know - Introduction to Perl

Page 1: The Perl You Need to Know - Introduction to Perl

The Perl You Need to Know Aaron Weiss

April 26, 1999

Perl - the very thought evokes a heady, adrenaline rush in the hearts of hard-core hackers, and an intimidating sense of anxiety in practically everyone else. The good news is that you don't need to master Perl to make it useful: Perl is simply a hammer, with which you can build a birdhouse or a mansion. And you don't need to be Bob Villa to build a birdhouse. In this article we begin a series on using Perl in web development scenarios. To start, we ease in with a general introduction to Perl itself. Future articles will take more in-depth looks at Internet capabilities you can leverage from within Perl programs. This article assumes Perl 5 as the reference version of Perl. Developers who have some familiarity with any other programming language, from BASIC to Pascal to C to JavaScript, will benefit the most from this article; however, readers with no programming background can still follow along and pick up most of the concepts.

"A minute to learn, a lifetime to master" is the slogan for the board game Othello, but it might just as well belong to Perl. Put more accurately, the basic concepts behind Perl are simple to understand, especially for developers familiar with other programming languages. Yet, the open-ended flexibility of Perl offers seemingly endless possibilities -- which is where the exhilaration and/or intimidation usually sets in. The good news is that you don't need to master Perl to make it useful. Web developers take heart: Perl is simply a hammer, with which you can build a birdhouse or a mansion. And you don't need to be Bob Villa to build a\ birdhouse.

In this article we begin a series on using Perl in web development scenarios. To start, we ease in with a general introduction to Perl itself. The purpose of this introduction is not to send you off writing enormous Perl programs -- there are plenty of articles and books which already succeed at this. Rather, this tutorial should arm you with a basic understanding of Perl so that you can work through the examples you care about -- web interaction -- without first needing to become a Perl guru. Future articles will take more in-depth looks at Internet capabilities you can leverage from within Perl programs. This article assumes Perl 5 as the reference version of Perl, which has supplanted the popular but older Perl 4 by several years now. Developers who have some familiarity with practically any other programming language, from BASIC to Pascal to C to JavaScript, will benefit the most from this article; however, readers with no programming background can still follow along and pick up most of the concepts by inference.

Contents:

Who's Afraid of Perl?Getting PerlRunning Perl ProgramsPerl Variables: ScalarsTable 1. Basic Perl OperatorsPerl Variables: Comparing ScalarsTable 2. Perl Comparison OperatorsPerl Variables: Lists (aka Arrays)Perl Variables: HashesPerl FunctionsPerl Program Flow: Conditionals and LoopsConclusion

Who's Afraid of Perl? Many Perl practitioners are zealots for the cause -- Perl has evolved into a near-religion for many programmers. The great advantage of this is that there are many folks out there who sincerely believe in the language and work hard on its behalf

Page 2: The Perl You Need to Know - Introduction to Perl

in continued support and development. The disadvantage of this zealotry is a somewhat Hare Krishna-esque style of evangelism which may intimidate many newcomers. Perl "mongers", as they like to be known, find great pleasure in maximizing their use of this tool, which often comes out in the form of incredibly cryptic Perl programs which pack extreme amounts of processing into small amounts of code.

A typical Perl monger, for example, will while away the better portion of a weekend in a darkened room and proudly produce the following gem:

$*=q^00$2b{29}2a;30$17>24&25*2a(1e)1b;28+1d-26/1b=28+22*1c(1e)17;19@21^, $*.="[1b]28";$z="";@%=split/\W/,$*;@_=map{hex}@%;for(@_){$_=chr($_+74);s%[zgf]%$z%g;my$t=$_;push@#,sub{$t};}for(@#){print&$_}0xf00f&print$/;

Scary stuff! What does it do? I have no idea! However, this is the kind of Perl that Perl mongers love to show off as to why Perl is so great. Unfortunately, well-meaning though they may be, Perl mongers often lack the ability to empathize with normal humans, to whom the above code provides every reason to find a different line of work. Such as botany.

Beginner's can take solace -- Perl programs need not be cryptic at all; in fact, they can be quite logical and easy to understand. Although such a style may not leverage the full power of Perl, it does wonders to improve the relationship between Perl and human, and this tutorial will definitely favor legible, clear code over the Perl monger style.

 

Why Perl and CGI

Web developers most frequently use Perl in the context of CGI. CGI has been overdescribed to the point of vagary and mystery for many new developers, but the concept is quite simple: the user provides some information on the web page and the browser sends this information to the web server. The web server passes this information to a particular program, this program "does a bunch of stuff" with the information, returns some results to the web server, which passes the results back to the user's browser.

CGI, which translates to "Common Gateway Interface", simply defines the means by which the user's data passes from the web server to the processing program and back. That's basically all you need to know about the internal guts of CGI.

The key idea to take away from this is that the program which "does a bunch of stuff" with the user's data can be written in any computer language which runs on the server and can communicate with the server via CGI.

Enter Perl -- Perl can "speak" CGI fluently and happens to be well-suited to manipulating data, which is exactly why Perl is the most common language used in developing CGI interactions.

CGI and Perl are such a powerful combination because, leveraged on the flexibility of Perl, almost any conceivable type of processing can constitute the "does a bunch of stuff" portion of the CGI interaction. Thus, when the Perl program receives the user's data it could follow any number of paths, from retrieving information from a database to constructing and delivering whole new web pages on-the-fly (known as "dynamic content").

But let's not overwhelm. First we'll look at using Perl to build some simple programs and then we'll see how to tie these programs into CGI interactions over the Web.

Getting Perl UNIX

Page 3: The Perl You Need to Know - Introduction to Perl

Of course, before you can write Perl programs, you'll need Perl itself. You may already have access to Perl depending on the type of system on which you develop.

If you develop on a Unix system, such as Solaris, SunOS, Linux, any BSD variant, or the many others, the chances are good that Perl is already installed. You can test this hypothesis quite simply -- login to your shell account (usually by telnet) and type:

perl -v

The output will look something like:

This is perl, version 5.004_04 built for i386-linux (with 1 registered patch, see perl -V for more detail) Copyright 1987-1998, Larry Wall

As you can see, this sample machine has Perl 5.004-04 installed. You do want to be sure that your UNIX system has at least a Perl version of 5.x; the previous version of Perl, 4.x, may still be installed on many systems. You don't want to use Perl 4 because it is old and unfashionable. A number of UNIX systems have both Perl 4 and Perl 5 installed. You may find that "perl" leads to Perl 4 but the command "perl5" leads to Perl 5. Thus, if "perl -v" failed you with a 4.x version, you might try:

perl5 -v

And see if that reports a 5.x version of Perl. If yes, then you should remember that your system refers to Perl 5 as "perl5" rather than simply "perl" -- this will be necessary to know later on.

If, for some odd reason, your UNIX system does not already have Perl installed, you will have to acquire Perl and have it installed. The installation would be done by the system administrator, if that is not yourself. If that is yourself, you can sometimes obtain ready-made binaries for various UNIX systems from your vendor (for instance, users of RedHat Linux can install the Perl RPM available on either the RedHat CD or Web site). The brave and courageous can also compile Perl from the source code, available from the Perl.com web site.

WINDOWS

Perl happily runs on other systems besides UNIX! If you develop in Windows, or would like to develop Perl programs in both Windows and UNIX environments, you can easily install the latest version of Perl. ActiveState maintains a Windows version of Perl which is quite easy to install, named ActivePerl.

MACOS

Despite its command-line heritage, Perl has even made it ashore on the Macintosh. Not that there's anything wrong with that. A variety of resources for the MacPerl project can be found the MacPerl 5 site.

Running Perl Programs A Perl program, as we'll soon see, is a text file containing Perl code. There are several ways to launch a Perl program, depending on your needs and the operating system in which you develop.

Page 4: The Perl You Need to Know - Introduction to Perl

Typically, you'll see that many Perl programs found on the net or in tutorials begin with the line:

#!/usr/bin/perl

The above line is actually intended for UNIX systems -- other operating systems will simply ignore it. However, because UNIX is typically considered the de facto platform for Perl, many Perl programs include this line. It tells UNIX where to find the Perl program; the path /usr/bin/perl is a common location for UNIX-based Perl installations. If you are working in a UNIX environment, it is important that this first line point to the correct location for Perl on your particular system; alternatives may look like, for example:

#!/usr/local/bin/perl#!/usr/bin/perl5

In either UNIX or Windows, if the perl program is in your search path you can also run a Perl program simply with the command line:

perl programName

Windows users might also use associations to run Perl scripts. Windows associations assign a particular program to run when a filename with a particular extension is launched. Thus, you can associate the perl executable with filenames ending, for example, in .plx or .pl. ActiveState's Perl installation will setup such an association for you. You can then launch Perl programs by simply double-clicking them from the desktop.

Keep in mind that for Web developers, many Perl programs will be launched in the context of CGI. We'll talk more about CGI later, but for now you should understand that using Perl programs for CGI involves a combination of the Web server's configuration and the above methods of launching Perl programs.

 

Show me the Perl: Greetings

A Perl script can be a very simple thing. The following program does only one thing: outputs the message "Welcome to Currency Central".

#!/usr/bin/perl

print "Welcome to Currency Central\n";

What could be simpler? The only bits in need of explanation here are two: first, note the \n. The backward slash (\) indicates an escape sequence, which refers to a special character that cannot easily be typed on the keyboard. The \n escape sequence represents a newline, causing there to be a linefeed in the output following the word "Central".

Also notice the semicolon which represents the end of a Perl statement. Not all Perl statements require semicolons at the end, but it is good practice, since omitting a semicolon where one is required will definitely result in a program error.

It is always a good idea to add comments to your program code, which make it easy to remember what a particular line does. Sometimes this is not obvious by simply reading the line of code. Comments in Perl begin with a pound sign (#), and continue to the end of that line.

#!/usr/bin/perl

#Output welcome messageprint "Welcome to Currency Central\n";

Simple enough, but this isn't exactly an impressive display of Perl power. Programming languages derive much of their power from the use of variables. Almost every developer has encountered variables in one scenario or another -- a variable is simply a "container" or label which possesses a certain value. We begin our study of Perl in earnest with a look at variables in Perl.

Page 5: The Perl You Need to Know - Introduction to Perl

Perl Variables: Scalars Variables allow us to manipulate values which, as the name implies, vary over time. Variables are used to contain values, be they arithmetic (numbers) or strings (a bunch of text characters), and variables can be operated upon, meaning added, subtracted, combined, multiplied, and so on.

The most common, basic variable in Perl is known as a scalar variable, and is represented by a preceding dollar sign ($). For example, imagine that we want to use a variable to keep the current exchange rate between American and Canadian currency, and this variable will be named "exRate". In Perl we would refer to this variable as:

$exRate

We could simply assign a value to exRate:

$exRate=1.35;

We could create another variable, $USTotal, to represent a dollar amount in American currency, then calculate the value of a resulting variable, $CDNTotal, and output the results.

#!/usr/bin/perl

$exRate=1.35;$USTotal=50.00;$CDNTotal=$USTotal*$exRate;print "$USTotal American dollars is equivalent ". "to $CDNTotal Canadian dollars.\n";

This simple program assigns values to two variables, and in the third line operates on them while assigning the results of that operation to a third variable. The operator used here is multiplication, represented by the asterisk (*), a typical operator in programming languages.

Notice that the scalar variable names appear within the double-quoted output string -- this may appear unusual to those familiar with other programming languages, but Perl recognizes that $USTotal and $CDNTotal are variable names and it substitutes their values in the output. The resulting output of the above program would look like:

Note the way the output strings have been broken up into multiple segments. Although we could have simply typed one long line, it would have been difficult to read within this article. To make the line more legible, the output string has been broken into portions, with a concatenation operator, the ".",between each portion. The result is exactly the same as if we typed one long line.

50 American dollars is equivalent to 67.5 Canadian dollars.

As you can see, a scalar variable holds one "piece" of information, known as a value. In other programming languages, there is strict control over the "types" of data that a value can represent. For example, a real number is an arithmetic value containing a decimal point (e.g. 25.75). An integer is a whole number (e.g. 25), while a string is a collection of alphanumeric characters (e.g. "hello 1999"). Whereas other languages may demand that you declare the type of value that a variable will contain, Perl is far less strict. In Perl, you can assign a scalar variable with any type of value you wish -- Perl will infer this value's type using its intelligence. Soon, we'll see why this is so useful.

When using scalar variables, you typically engage in one of two types of actions: operations and comparisons.

Perl Variables: Operating on Scalars

We've already seen one example of operating on some scalar variables -- recall line 3 of the previous example:

$CDNTotal=$USTotal*$exRate;

Page 6: The Perl You Need to Know - Introduction to Perl

There are two operations here: the multiplication operator, represented by the asterisk (*), multiplies the value of $USTotal by the value of $exRate. The assignment operator, represented by the equal sign (=), assigns the result of the multiplication operation to the variable $CDNTotal.

Remember previously we opined that Perl uses its intelligence to infer the type of value a variable contains. For instance, when you multiply two variables, this operation only makes sense if the two variables contain numeric values. You can't, after all, multiply the value "chicken" by the value "eggs". Thus, when you use an arithmetic operator, Perl tries its hardest to use the variable's values as numbers.

Some operators, though, are not arithmetic. For example, consider the string concatenation operator, represented by the dot character (.). String concatenation is the result of squishing together two string values; for instance:

"hello" . "goodbye" yields "hellogoodbye"

Here we come to the important point: if you use an arithmetic operator on two values which can be seen as numbers, Perl will perform the arithmetic. If you use a string operator on the two values, Perl will treat the values as strings. Thus:

5 + 10 yields 15

5 . 10 yields "510"

Page 7: The Perl You Need to Know - Introduction to Perl

Table 1. Basic Perl Operators The table below summarize the common Perl operators used in arithmetic and string operations.

Table 1. Basic Perl Operators

Operator Name Description Example Result

+ addition Sums values together. 20+30 50

- subtraction Subtracts right value from left value. 20-10 10

* multiplication Multiplies values together. 20*30 600

/ division Divides left value by right value. 20/5 4

% modulus Remainder of dividing left value by right value. 20/3

220 divided by 3 yields 6 (6*3=18) with a remainder of 2 (20-18).

** exponential Raises the left value to the power of the right value. 20**2 400

Equivalent to 20*20

. string concatenation

Chains together the characters in each value.

"hot"."dog""hot "."dog"

"hotdog""hot dog"

x string replicate

Repeats the characters in left value the right value number of times.

"*"x5"*"x5."Hello"."*"x5

**********Hello*****

Perl Variables: Comparing Scalars Size counts, often, when you must weigh one value against another. Returning to our currency conversion example, you may wish to determine whether the Canadian dollar figure is a greater or lesser number than its American dollar equivalent. When you compare two values in Perl, the result is either true or false. Using comparison operators you specify the criterion for the comparison.

For example, suppose that $USTotal contains the value 50 and $CDNTotal possesses 67.5. Using the arithmetic greater-than operator (>):

$USTotal > $CDNTotal yields false

$CDNTotal > $USTotal yields true

Typically, you would include one of these comparisons inside a larger expression, which makes use of the result. A simple example would be an assignment to a third variable:

$USgreater=($USTotal>$CDNTotal);

In a later line of code you might test whether $USgreater contained true or false, as a result of the above comparison. More commonly, though, these types of comparisons are used in conditional statements to direct the flow of the Perl program. We haven't yet looked at conditional statements, but they should be quite familiar to readers with basic programming experience. Non-programmers can simply read the example aloud to get an idea of the logic behind this code:

Page 8: The Perl You Need to Know - Introduction to Perl

if ($USTotal>$CDNTotal) { ...execute some piece of code... }else { ...execute some other piece of code... }

Now suppose that we wanted to test whether $USTotal and $CDNTotal were equal. Using the arithmetic equality operator, a double equal sign (==):

$USTotal == $CDNTotal yields false

Of course, if both $USTotal and $CDNTotal contained the same value, the result of this equality comparison would have been true. Programmers note: the arithmetic comparison operator is a double equal sign, not just one. Several other programming languages use a single equal sign to represent both the comparison operator and the assignment operator, and forgetting to use the double equal sign is a common cause of program errors in Perl scripts.

Recall our earlier discussion about data types, and how Perl infers the data type based on the operator being used. In the above example, we are comparing whether one value is greater than another -- this only makes sense with numbers, since we cannot say that "dog" is greater than "cat" (well, some might say that, but they would be asking for trouble). In fact, Perl can make this comparison, but it does so using different logic and different operators.

As far as Perl is concerned, "dog" is greater than "cat" because it comes after cat alphabetically. Conversely, we can say that "cat" is less than "dog". The string greater-than operator in Perl is "gt", and the string equality operator is "eq", thus if $petA contained "cat" and $petB contained "dog" we could compare:

$petA gt $petB yields false

$petA eq "cat" yields true

There are many possible reasons to compare variables -- this tutorial is not in-depth enough to explore these in detail. The table below summarizes the common comparison operators, which you can use when and if you encounter a situation where such a comparison would be useful.

Table 2. Perl Comparison Operators

Page 9: The Perl You Need to Know - Introduction to Perl

Operator Name Description Example Result

> arithmetic greater-than

Compares whether left value is greater than right value. 20>30 false

< arithmetic less-than

Compares whether left value is smaller than right value. 20<30 true

>= arithmetic greater-than or equal to

Compares whether left value is equal to or greater than right value. 30>=20 true

<= arithmetic less-than or equal to

Compares whether left value is equal to or less than right value 20<=20 true

== arithmetic equality

Compares whether left value is equal to right value. 20==30 false

!= arithmetic inequality

Compares whether left value is not equal to right value. 20!=30 true

gt string greater-than

Compares whether left value comes alphabetically after right value. "fat" gt "hat" false

lt string less-than

Compares whether left value comes alphabetically before right value. "fat" lt "hat" true

ge string greater-than or equal to

Compares whether left value is equal to or comes alphabetically after right value.

"pat" ge "pat" true

le string less-than or equal to

Compares whether left value is equal to or comes alphabetically before right value.

"sat" le "pat" false

eq string equality

Compares whether left value is exactly the same as the right value. "bob" eq "bob1" false

ne string inequality

Compares whether left value is at all different from the right value. "bob" ne "bob1" true

Perl Variables: Lists (aka Arrays)

Page 10: The Perl You Need to Know - Introduction to Perl

So far we've been deluged with information on scalar variables, certainly the most commonly used type of variable in Perl (or any other programming language for that matter). Runner-up in this popularity contest, though, are lists, also commonly known as arrays in other languages. Lists, in brief, allow one variable label to contain a bunch of values, rather than merely one.

We use list variables frequently in real life. A shopping list contains a bunch of values -- names of items. A budget is a list of arithmetic values -- expenditures.

List variables are quite different from scalar variables because they do not share the same types of operations. After all, it doesn't make much sense to compare whether a list of colors is "greater" than a list of pet names (it does make sense to compare whether one list is longer than another, but then the length of a list is a scalar variable).

To prevent variable name confusion, lists in Perl are prefixed with the at-sign character (@). The values contained in a list are called items, and assigning a list of items to a list is quite simple:

@shopping=("milk","eggs","bread","cheetos");

Now then, the list @shopping contains four items, indexed in the order they were created. The first item in the list is "milk", the second is "eggs", and so on. Important: the first item in a list is counted as index number zero. Restated, item 0 in this list is "milk", item 1 is "eggs", and so on.

Here's the confusing bit: when working with lists, we sometimes work with the list as a whole and other times work with particular items from the list. Working with a single item from a list is a scalar activity, as we saw in the earlier sections. You can reference a single item from a list using its index:

$shopping[0]="skim milk";

The above variable refers to the first item in the @shopping list, which has been assigned a new value, "skim milk". Notice, though, that this reference used a $ rather than an @ -- because this one item in the list is scalar. What if you wanted to work with more than one item from the list, but not the whole list? That, then, would constitute a slice, because it is still a list (a list of a list!):

@shopping[0,3]=("skim milk","diet cheetos");

In this example we've assigned new values to two of the items in the list, the first and last items. This time we stuck with the @ prefix, because we have used a slice of the list.

It is quite important to use the correct notation, scalar or list, when constructing these references. Sometimes, using the wrong notion, such as a $ where an @ should be, won't result in an error, per se, but in incorrect results within the program. This is because Perl will interpret the expression one way or another depending on whether you intended to "see" the variable as a list or a scalar at a given moment.

You can determine the length of a list -- that is, how many items it has, using the scalar reference:

$#shopping

The above returns the number of the highest index in the list; in this example, that would be 3. Remember that the list begins at index 0, so a high index of 3 means that this list contains 4 items. If you've ever wondered why programmers are, well, the way they are ... just remember that they have to deal with this kind of thing day in and day out!

Lists provide a convenient means for many types of data sorting, but, again, this tutorial is very much a starter course. Once familiar with the basics of these Perl fundamentals, the twists and turns that come down the road will be that much less dizzying.

Perl Variables: Hashes

Page 11: The Perl You Need to Know - Introduction to Perl

The third and last type of traditional variable that we'll look at in Perl is the hash. Hashes are conceptually similar to lists in some respects, but offer a different and highly useful extra functionality.

Recall our shopping list -- in its original form, this data was suited to a Perl list because it was merely an arbitrary list of items. But would this list be useful in the real world? If Susan were to hand her husband Melvin this list, he might pick up

milk, but which milk would he buy? Perhaps Susan would want Melvin to purchase the brand of milk on sale this week, meaning that the specific brand to buy may change from week to week. In this case, the grocery list must be expanded so that it can relate the items to some additional information; e.g.

"milk" is related with "Holy Cow Brand 1%""eggs" is related with "Store Brand Large""bread" is related with "Smurf Bakery Light Wheat"

The above data, in Perl parlance, is known as a set of key-value pairs. On the left side are the keys ("milk","eggs", and "bread") and on the right side are the values. A hash, then, is actually a list of key-value pairs. Hashes are referred to with the prefix %. Creating the initial hash for this grocery list is simple:

%shopping=("milk","Holy Cow 1%", "eggs","Store Brand Large", "bread","Smurf Bakery Light Wheat");

As with lists, any single item in the hash is referred to as a scalar variable. Unlike lists -- and this is where the power of hashes come in -- the order of the items is unimportant. Hashes do not use indexes to see their data, they use the keys. So, if we wanted to see the value for the key "milk":

print $shopping{"milk"};

The above line would output "Holy Cow 1%". Next week, when the sales change, Susan can easily assign a different milk brand value to the milk key:

$shopping{"milk"}="Heffer 2000 Skim";

You can add new key-value pairs to the hash at any time using a simple assignment:

$shopping{"dessert"}="HunkaHunka Premium Ice Cream";

Hashes are incredibly useful when you want to create a list but you also want to look up data from the list using related bits of data, rather than meaningless index numbers. Admittedly, most of the people in charge of the weekly grocery list don't manage it using Perl hashes -- but two quite practical examples quickly spring to mind:

1. A translation table. Imagine that you are looking up data from a database, and you have used field names to look up the data. So, the field "RINV" might represent the "Remaining Inventory" field. When you output this data to the screen you might want to reproduce the field labels, but "RINV" is rather cryptic. It would be easy to setup a hash that translates the database field names into human-readable field names:

2. %fields("RINV","Remaining3. Inventory","TINV","Total Inventory");4. print "$fields{'RINV'}: $ValueFromDatabase\n";

5. Frequency analysis. You are a celebrity linguist designing a word-frequency analyzer to help determine how often your own name appears in newspaper articles. Each unique word counted is entered as the key of a hash; the number of instances of this word is updated as the value of the hash. First, initialize an empty hash table:

%freq=();

Imagine that $word represents the most recent word read in from the source data:

Page 12: The Perl You Need to Know - Introduction to Perl

$freq{$word}++;

The above line increments the value of the specified key. Requesting the frequency of the word "happy" then becomes trivially easy:

$freq{"happy"}

Page 13: The Perl You Need to Know - Introduction to Perl

Perl Functions A function is like sending out for pizza ... you call a third-party (the pizza shop), provide some parameters (your name, your order), and in 30 minutes or less the results appear at the door. Functions, though, work much more quickly and don't need to be tipped.

Perl comes with many, many functions built-in. These functions range widely in use, from manipulating numbers and strings to working with files on disk. Some functions require extra information to tell them how to do their job -- these are called parameters. Some functions are used inside of larger expressions while others can stand alone. Many functions can be any of the above.

Consider the humble lc function. This function accepts one parameter, a string, and returns a copy of the string in all lowercase letters:

$str="HELLO";$lowerStr=lc($str);

In the above, the variable $lowerStr would contain the value "hello". Notice how the lc function is used within an assignment operator; after all, using lc by itself...

lc($str);

...would not achieve anything since its returned value has nowhere to go and would evaporate. Notice, though, that the lc function does not change the content of $str at all -- it merely returns a modified copy of $str. Some functions act directly on the variable passed to them -- these functions happily stand alone. For example, the chop function hacks off the last character in a string:

$str="one,two,three,";chop($str);

The chop call actually modifies the value of $str to "one, two,three". Although chop can be used standalone, that doesn't mean it doesn't return any information. In fact, if used in an expression, chop returns the character that it severed off the original string. Thus:

$str="one,two,three,";$chopped=chop($str);

The above would both chop the final comma from $str and assign that comma to $chopped. Typically there is no need to save the hacked off part of the string, which is why chop is commonly used without being part of a larger expression.

Functions don't necessarily have to accept their parameters inside parentheses. You may well see examples of functions which are called without any parentheses, such as "chop $str". In fact, you've already seen such an example in this article -- the print function. As a rule of thumb, though, it is best to keep using the parentheses to enclose parameters being passed to the function -- print is an exception in that it is widely accepted without parentheses. Perl lets you get away with much syntactical sloppiness, but that doesn't mean it is a good idea.

Using functions is straightforward, and an essential part of Perl. The good news is that Perl includes a huge number of built-in functions ready and waiting to make programming easier, from handling mathematic calculations to hacking up strings into various bits and pieces to managing times and dates to writing to and reading from data files. The bad news is that there are simply too many functions to summarize in this article. The extra good news is that these functions have already been summarized and categorized, and you can find them in the perlfunc documentation at CPAN, the Comprehensive Perl Archive Network.

Page 14: The Perl You Need to Know - Introduction to Perl

Perl Program Flow: Conditionals and Loops

By default, Perl programs, like many other languages, execute in the order the code is written, from top to bottom. This order of execution is known as "program flow" in textbooks, and there are a variety of reasons why we might want to alter the program flow under certain circumstances. Again, consider the parallels to real life -- you may plan out your day in a certain order, but variable circumstances may change this order. For instance, your day plan might look like:

if (bank is open) { go to bank }else { go to grocery; go to bank }

The use of conditional logic allows your schedule to become more flexible -- go to the bank first if it is open, otherwise go to the grocery and then go to the bank. Similarly, we apply the same sort of logic to the flow of Perl programs. The two main controls which alter program flow are conditionals and loops.

Conditionals, like the if...else statement seen in the above example, determine what actions to take when certain conditions are met or are not met.

Loops are used to repeat one or more actions a certain number of times based upon certain conditions.

if and unless statements

One of the most intuitive conditionals is the if statement. You can read this example aloud to make the logic perfectly clear:

if (some condition) { ...do some actions when true... }

Notice that the actions clause is enclosed within curly braces. A set of curly braces is known as a statement block, and any number of Perl statements can appear here, separated by semicolons. Recalling our currency conversion example, we can output a message sensitive to which currency is more valuable than the other:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }

The else clause of an if statement lets you define a statement block in case the conditional test is false:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }else { print "Presently, the Canadian dollar is ". "worth more than the US dollar.\n" }

Unfortunately, the above example is flawed. It is theoretically possible that both currencies are of equal value. The elsif clause acts as a combined else and if

Note the way the output strings have been broken up into multiple segments. Although we could have simply typed one long line, it would have been difficult to read within this article. To make the line more legible, the output string has been broken into portions, with a concatenation operator, the ".", between each portion. The result is exactly the same as if we

Page 15: The Perl You Need to Know - Introduction to Perl

clause, allowing an else clause which tests an additional condition. This may be clearer by example:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }elsif ($USTotal==$CDNTotal) { print "Presently, both the US dollar and ". "the Canadian dollar are of equal value.\n" }else { print "Presently, the Canadian dollar is ". "worth more than the US dollar.\n" }

typed one long line.

The statement above actually tests two conditions: if the US dollar is worth more then the first message is output; if the two are equal then the second message is output; the process of elimination tells us that if neither of these were true than the US dollar must be worth less, so the third message it output.

A few notes on syntax: the formatting and indentation used in these examples is merely one style, albeit a style this author finds intuitive and legible. Other programmers may favor different styles of indentation -- Perl is very flexible and essentially lets you format statements and statement blocks any way you like. Also, note that the elsif clause of this statement is spelled strangely, missing the second "e" that it seems it should have. Perl can be quirky.

A related variation of the if statement is the unless statement. This statement merely tests if the given condition is not true:

unless (bank is open) { go to grocery; go to bank }else { go to bank; go to grocery }

In this logic, if the bank is not open then we go shopping first; otherwise the bank is open and we go to the bank first. The unless statement can sometimes be confusing to work through mentally, though it might help to think of it as meaning "if the condition is not true". In every other way unless behaves like the if statement, and can take additional elsif and/or else clauses.

while, for, and foreach loops

"Lather, rinse, repeat" -- the insightful instructions on the label of shampoo bottles. Yet, this bit of cleansing wisdom sheds light on one of the most fundamental techniques in Perl programming -- the loop. In fact, we can code the shampoo mantra using Perl's while loop:

while (hairISgreasy) { lather; rinse }

The shampoo loop starts with a conditional test: is the hair greasy? A statement block follows this test, and this block is executed if the condition is true. Thus, if the hair is greasy, lather and rinse. When the statement block is completed, the while loop control returns to the test at the top of the loop. Is the hair greasy? If yes, the loop iterates again, once more lathering and rinsing. This loop will iterate ad infinitum until the while condition evaluates to false. If, on the third iteration, the hair is no longer greasy, the loop ends -- the statement block is not executed and program flow drops to the next statement after the while loop's statement block. If the while condition is false the first time the loop is evaluated, the statement block is never executed.

The for loop is similar in nature to the while loop but tends to be used when counting is important. A basic example would be a for loop which counts from 1 to 10:

Page 16: The Perl You Need to Know - Introduction to Perl

for ($j=1; $j<=10; $j++) { print "$j\n" }

The for statement takes a three-part control:. First, $j=1 sets the counting variable for this loop, $j, to a value of 1. You can use any variable name you wish, but $i and $j are historically traditional loop counters. The second part of the for loop's control is the conditional -- in this case, is $j less-than or equal to the value 10? The third part of the control advances the counter. Here we use the common autoincrement operator which simply adds 1 to the current value of $j.

The first time through the loop, $j contains 1, and thus 1 is output by the print function. The second time through the loop $j is incremented to 2, is compared against 10, and again is output by the print function. This process simply repeats until $j increments to 11, which of course is greater than 10. At that point the loop exits and Perl passes over the statement block and moves onto the next statement in the program.

When dealing with lists, the foreach loop provides a simple way to iterate through each item in the list. Recall our grocery list from earlier, @shopping. Suppose you wanted to work with each item in @shopping:

foreach $shopping_item (@shopping) { do something with $shopping_item }

This foreach loop does not take any conditional. Rather, it iterates for each item in @shopping. In each iteration, the variable $shopping_item contains the next value in @shopping. Thus, the first time through the loop $shopping_item is "milk"; the second time through $shopping_item is "bread", and so on. Within the statement block of the foreach loop you can modify the value of $shopping_item, if need be, without affecting the original value in @shopping.

Perl Program Flow: Conditionals and Loops

By default, Perl programs, like many other languages, execute in the order the code is written, from top to bottom. This order of execution is known as "program flow" in textbooks, and there are a variety of reasons why we might want to alter the program flow under certain circumstances. Again, consider the parallels to real life -- you may plan out your day in a certain order, but variable circumstances may change this order. For instance, your day plan might look like:

if (bank is open) { go to bank }else { go to grocery; go to bank }

The use of conditional logic allows your schedule to become more flexible -- go to the bank first if it is open, otherwise go to the grocery and then go to the bank. Similarly, we apply the same sort of logic to the flow of Perl programs. The two main controls which alter program flow are conditionals and loops.

Page 17: The Perl You Need to Know - Introduction to Perl

Conditionals, like the if...else statement seen in the above example, determine what actions to take when certain conditions are met or are not met.

Loops are used to repeat one or more actions a certain number of times based upon certain conditions.

if and unless statements

One of the most intuitive conditionals is the if statement. You can read this example aloud to make the logic perfectly clear:

if (some condition) { ...do some actions when true... }

Notice that the actions clause is enclosed within curly braces. A set of curly braces is known as a statement block, and any number of Perl statements can appear here, separated by semicolons. Recalling our currency conversion example, we can output a message sensitive to which currency is more valuable than the other:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }

The else clause of an if statement lets you define a statement block in case the conditional test is false:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }else { print "Presently, the Canadian dollar is ". "worth more than the US dollar.\n" }

Unfortunately, the above example is flawed. It is theoretically possible that both currencies are of equal value. The elsif clause acts as a combined else and if clause, allowing an else clause which tests an additional condition. This may be clearer by example:

if ($USTotal>$CDNTotal) { print "Presently, the US dollar is ". "worth more than the Canadian dollar.\n" }elsif ($USTotal==$CDNTotal) { print "Presently, both the US dollar and ". "the Canadian dollar are of equal value.\n" }else { print "Presently, the Canadian dollar is ". "worth more than the US dollar.\n" }

Note the way the output strings have been broken up into multiple segments. Although we could have simply typed one long line, it would have been difficult to read within this article. To make the line more legible, the output string has been broken into portions, with a concatenation operator, the ".", between each portion. The result is exactly the same as if we typed one long line.

The statement above actually tests two conditions: if the US dollar is worth more then the first message is output; if the two are equal then the second message is output; the process of elimination tells us that if neither of these were true than the US dollar must be worth less, so the third message it output.

A few notes on syntax: the formatting and indentation used in these examples is merely one style, albeit a style this author finds intuitive and legible. Other programmers may favor different styles of indentation -- Perl is very flexible and essentially lets you format statements and statement blocks any way you like. Also, note that the elsif clause of this statement is spelled strangely, missing the second "e" that it seems it should have. Perl can be quirky.

Page 18: The Perl You Need to Know - Introduction to Perl

A related variation of the if statement is the unless statement. This statement merely tests if the given condition is not true:

unless (bank is open) { go to grocery; go to bank }else { go to bank; go to grocery }

In this logic, if the bank is not open then we go shopping first; otherwise the bank is open and we go to the bank first. The unless statement can sometimes be confusing to work through mentally, though it might help to think of it as meaning "if the condition is not true". In every other way unless behaves like the if statement, and can take additional elsif and/or else clauses.

while, for, and foreach loops

"Lather, rinse, repeat" -- the insightful instructions on the label of shampoo bottles. Yet, this bit of cleansing wisdom sheds light on one of the most fundamental techniques in Perl programming -- the loop. In fact, we can code the shampoo mantra using Perl's while loop:

while (hairISgreasy) { lather; rinse }

The shampoo loop starts with a conditional test: is the hair greasy? A statement block follows this test, and this block is executed if the condition is true. Thus, if the hair is greasy, lather and rinse. When the statement block is completed, the while loop control returns to the test at the top of the loop. Is the hair greasy? If yes, the loop iterates again, once more lathering and rinsing. This loop will iterate ad infinitum until the while condition evaluates to false. If, on the third iteration, the hair is no longer greasy, the loop ends -- the statement block is not executed and program flow drops to the next statement after the while loop's statement block. If the while condition is false the first time the loop is evaluated, the statement block is never executed.

The for loop is similar in nature to the while loop but tends to be used when counting is important. A basic example would be a for loop which counts from 1 to 10:

for ($j=1; $j<=10; $j++) { print "$j\n" }

The for statement takes a three-part control:. First, $j=1 sets the counting variable for this loop, $j, to a value of 1. You can use any variable name you wish, but $i and $j are historically traditional loop counters. The second part of the for loop's control is the conditional -- in this case, is $j less-than or equal to the value 10? The third part of the control advances the counter. Here we use the common autoincrement operator which simply adds 1 to the current value of $j.

The first time through the loop, $j contains 1, and thus 1 is output by the print function. The second time through the loop $j is incremented to 2, is compared against 10, and again is output by the print function. This process simply repeats until $j increments to 11, which of course is greater than 10. At that point the loop exits and Perl passes over the statement block and moves onto the next statement in the program.

When dealing with lists, the foreach loop provides a simple way to iterate through each item in the list. Recall our grocery list from earlier, @shopping. Suppose you wanted to work with each item in @shopping:

foreach $shopping_item (@shopping) { do something with $shopping_item }

This foreach loop does not take any conditional. Rather, it iterates for each item in @shopping. In each iteration, the variable $shopping_item contains the next value in @shopping. Thus, the first time through the loop $shopping_item is "milk"; the second time through $shopping_item is "bread", and so on. Within the statement block of the foreach loop you can modify the value of $shopping_item, if need be, without affecting the original value in @shopping.

Page 19: The Perl You Need to Know - Introduction to Perl

Conclusion For many people, the fun of driving a car is not the movement of one's foot from gas pedal to brake pedal, or the method for turning on the wipers or shifting into reverse. The fun of driving, for most, is the travel -- going from one place to another, the scenery, and of course the speed. Still, there is a smaller hard-core segment of folks who are enamored with shifting gears and how the brakes work.

Perl is quite the same -- Perl enthusiasts love getting their hands covered with grease, but you can get from A to B with Perl without needing to know how to rebuild its transmission! This article has tried to survey a wide range of territory towards this end. A web developer is primarily interested in using Perl as a tool towards web-related goals. In doing so, the web developer will need a basic understanding of the principles of Perl programming, and that has been the focus of much of this introductory piece.

Using Perl with CGI to create interactive and dynamic web pages is the fun part of all this. But you can't cruise the interstate after your first driver's ed lesson and, similarly, we've only seen a glimmer of the fun Perl can offer on the web. The stage has been set, though, and next time we can put this Perl into drive, from validating form submissions to processing database requests to shopping cart applications. Vroom.

Additional Reading -- Some Opinionated Thoughts

Despite the perilous pace of technology, there are still some tasks better done with old fashioned printed books. Learning Perl is one of them. Books are convenient to reference, are well bound, and easy to carry from bedroom to, er, bathroom. That said, if you ask a typical Perl purveyor what book to read you'll undoubtedly be sent the way of "The Llama", which is an affectionate moniker for "Learning Perl", by Randal Schwartz and Tom Christiansen, one of the series of O'Reilly and Associates books with strange animal prints on the cover.

Allow me to dissent. Although "The Llama" is the de facto recommendation among the Perl mongers, it is not an ideal book with which to begin the Perl journey. A reader who wishes to gain a solid understanding of Perl, coming with little to moderate programming and/or Unix experience, one would assert that "Learning Perl" further contributes to the intimidating and cliquish atmosphere surrounding this powerful and useful language.

In fact, at the risk of eternal banishment from the Perl Kingdom Hall and a spate of frighteningly obtuse flame-mails, one would posit that a beginner to Perl would be better served by -- yes -- "Perl 5 for Dummies", by Paul Hoffman. Out on the limb this far, one would go even further to say that the Dummies book presents a much clearer organization of Perl information and explains the essential concepts with far more clarity, and far less ego, than the exalted Llama.

In self-defense, it should be noted that the authors of "Learning Perl", Mr. Schwartz and Mr. Christiansen most certainly know their stuff -- they are true high priests of Perl. Their writings provide great insight into the workings of Perl and into its advanced usage, and are highly recommended for those at such a level of interest. Simply put, though, a newcomer to the language will gain a more useful understanding from the clarity and structure of "Perl 5 for Dummies". And it should be noted that there are no invested ties to either of these opinions.

This is no indictment of all animal-print covers -- in fact, one does recommend that any level of Perl programmer would benefit from O'Reilly's "Perl in a Nutshell", by Ellen Siever, Stephen Spainhour, and Nathan Patwardhan (featuring a camel head cover). Although it contains little instruction or narrative, this book is an excellent reference resource summarizing all aspects of the Perl language, from operators to functions to the many popular modules.

Page 20: The Perl You Need to Know - Introduction to Perl

Resources

The Perl Documentation (thorough!)  Selena Sol's Introduction to Perl CGI Programming 101, by Jacqueline D. Hamilton CGI Module Reference (technical) Summary and Documentation of Standard Perl Modules