Perl 5 By Example

Peter Kitson

ISBN : 0789708663

Order a printed copy of this book from Amazon.

Cover Design - Perl 5 By Example

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)



Sample Chapter From Perl 5 By Example
     Copyright © David Medinets

Getting Your Feet Wet

You are about to embark on a journey through the world of Perl programming. You\'ll find that the trip has been made easier by many examples liberally sprinkled along the trail. The beginning of the trip covers the basic concepts of the Perl language. Then you move on to some of the more advanced concepts - how to create Perl statements and whole programs. At the end of the trip, some guideposts are placed - in the form of Internet sites - to show you how to explore more advanced programming topics on your own.

Do you know any other programming languages? If so, then learning Perl will be a snap. If not, take it slow, try all of the examples, and have fun experimenting as you read.

I thought about adding a section here about programming ideals. Or perhaps, a discussion about the future of Perl. And then I realized that when I was first learning computer languages, I didn\'t really care about that stuff. I just wanted to know about the language and what I could do with it.

With that in mind, the next section on Perl\'s origin is very short. After all, you can read all the background information you\'d like using a web browser by starting at - the Perl home page.


Perl began as the result of one man\'s frustration and, by his own account, inordinate laziness. It is a unique language in ways that cannot be conveyed simply by describing the technical details of the language. Perl is a state of mind as much as a language grammar.

One of the oddities of the language is that its name has been given quite a few definitions. Originally Perl meant the Practical Extraction Report Language. However, programmers also refer to is as the Pathologically Eclectic Rubbish Lister. Or even, Practically Everything Really Likeable.

Let\'s take a few minutes to look at the external forces which provoked Perl into being - it should give you an insight into the way Perl was meant to be used. Back in 1986, Larry Wall found himself working on a task which involved generating reports from a lot of text files with cross references. Being a UNIX programmer, and because the problem involved manipulating the contents of text files, he started to use awk for the task. But it soon became clear that awk wasn\'t up to the job; with no other obvious candidate for the job, he\'d just have to write some code.

Now here\'s the interesting bit: Larry could have just written a utility to manage the particular job at hand and gotten on with his life. He could see, though, that it wouldn\'t be long before he\'d have to write another special utility to handle something else which the standard tools couldn\'t quite hack. (It\'s possible that he realized that most programmers were always writing special utilities to handle things which the standard tools couldn\'t quite hack...)

So rather than waste any more of his time, he invented a new language and wrote an interpreter for it. If that seems like a paradox, it isn\'t really - it\'s always a bit more of an effort to set yourself up with the right tools, but if you do it right, the effort pays off.

The new language had an emphasis on system management and text handling. After a few revisions, it could handle regular expressions, signals, and network sockets too. It became known as Perl and quickly became popular with frustrated, lazy UNIX programmers. And the rest of us.

Is it "Perl" or "perl"? The definitive word from Larry Wall is that it doesn\'t matter. Many programmers like to refer to languages with capitalized names (Perl) but the program originated on a UNIX system where short, lower-case names (awk, sed, and so forth) were the norm. As with so many things about the language, there\'s no single "right way" to do it; just use it the way you want. It\'s a tool, after all, not a dogma.

If you\'re sufficiently pedantic, you may want to call it "[Pp]erl" after you\'ve read Chapter 10, "Regular Expressions."

Similar to C?

Perl programs bear a passing resemblance to C programs, perhaps because Perl was written in C, or perhaps because Larry found some of its syntactic conventions handy. But Perl is less pedantic and a lot more concise than C.

Perl can handle low-level tasks quite well, particularly since Perl 5, when the whole messy business of references was put on a sound footing. In this sense it has a lot in common with C. But Perl handles the internals of data types, memory allocation and such automatically and seamlessly.

This habit of picking up interesting features as it went along - regular expressions here, database handling there - has been regularized in Perl 5. It is now fairly easy to add your favorite bag of tricks to Perl by using modules. It is likely that many of the added - on features of Perl such as socket handling will be dropped from the core of Perl and moved out to modules after a time.

Cost and Licensing

Perl is free. The full source code and documentation are free to copy, compile, print, and give away. Any programs you write in Perl are yours to do with as you please; there are no royalties to pay and no restrictions on distributing them as far as Perl is concerned.

It\'s not completely "public domain," though, and for very good reason. If the source were completely public domain, it would be possible for someone to make minor alterations to it, compile it, and sell it - in other words, to rip off its creator. On the other hand, without distributing the source code, it\'s hard to make sure that everyone who wants to can use it.

The GNU General Public License is one way to distribute free software without the danger of someone taking advantage of you. Under this type of license, source code may be distributed freely and used by anybody, but any programs derived using such code must be released under the same type of license. In other words, if you derive any of your source code from GNU-licensed source code, you have to release your source code to anyone who wants it.