9781118013847
whatapostrophes_perlquestion_mark.html

Chapter 1. What’s Perl?

WHAT YOU WILL LEARN IN THIS CHAPTER

  • What is Perl?

  • The History of Perl

  • Getting Perl

  • Learning the community

  • Understanding the Perl documentation

  • Using a terminal

My goodness, where to start? To even begin to cover a language with such a rich history and huge influence over the history of computing and the Web is a daunting task, so this chapter will just touch on the highlights. By the time you’re finished with it, you’ll have a good understanding of the history of Perl and where to go to get more help when you need to know more than this book can offer. Learning how to find the answers to your questions is probably one of the most valuable skills you can develop.

The History of Perl

You can skip this section if you like, but I wouldn’t. Knowing the history of a language can give you insight into why it has the quirks it does and where it’s heading. Perl has a long and interesting history, but we’ll not bore you too much with detail. We’ll just cover the highlights you should know.

Perl’s Humble Beginnings

Perl was created by Larry Wall, who had previously released programs used by every system administrator (patch) or casual Unix user (rn news reader, the closest thing to a web browser in those days). As for the Perl language, we actually don’t know when it was first created. In the perlhist document that ships with Perl, Larry lists the creation data of Perl 0 as “Classified”. However, Perl 1.000 was released to the world on December 18th, 1987, well over 20 years ago. Larry probably didn’t realize the huge impact this language would have. Here’s the beginning of the 11 page manpage (manual) that was originally included with Perl:

NAME
    perl | Practical Extraction and Report Language

SYNOPSIS
    perl [options] filename args

DESCRIPTION
    Perl is a interpreted language optimized for scanning  arbi-
    trary  text  files,  extracting  information from those text
    files, and printing reports based on that information.  It's
    also  a good language for many system management tasks.  The
    language is intended to be practical  (easy  to  use,  effi-
    cient,  complete)  rather  than  beautiful  (tiny,  elegant,
    minimal).  It combines (in  the  author's  opinion,  anyway)
    some  of the best features of C, sed, awk, and sh, so people
    familiar with those languages should have little  difficulty
    with  it.  (Language historians will also note some vestiges
    of csh, Pascal, and  even  BASIC|PLUS.)   Expression  syntax
    corresponds  quite  closely  to C expression syntax.  If you
    have a problem that would ordinarily use sed or awk  or  sh,
    but  it exceeds their capabilities or must run a little fas-
    ter, and you don't want to write the silly thing in C,  then
    perl  may  be  for  you.  There are also translators to turn
    your sed and awk scripts  into  perl  scripts.   OK,  enough
    hype.

Note

The name of the language causes a bit of confusion. When someone writes “Perl” (upper case) they are referring to the programming language you will be learning in this book. When someone writes “perl” (lower case) they are referring to the binary executable that is used to run “Perl”, the language.

So “perl” is the binary and “Perl” is the language. The former parses and runs the latter: perl parses and runs Perl. If someone writes PERL, you know immediately that they’re not very familiar with the Perl language. This is why sometimes you’ll see experienced programmers use PERL to refer to poorly written Perl programs.

Due to the wording of the original documentation that shipped with Perl, many programmers assume that PERL is an acronym for “Practical Extraction and Report Language”. However perlfaq1 (documentation shipped with Perl that will be explained in a bit) sets the record straight:

... never write "PERL", because perl is not an acronym, apocryphal
 folklore and post-facto expansions notwithstanding.

Remember: there is no such thing as PERL. It’s Perl, the language, or perl, the executable.

From that humble beginning, Perl grew rapidly. Version 2.000 was released in June of 1988. Version 3.000 was released in October, 1989. In March of 1991, Perl 4 was released, but that was largely the same as Perl 3 with the version number bumped up to make it clear which version O’Reilly’s “Programming Perl” (a.k.a., “The Camel Book”) was referring to.

By the early 90s, Perl had become extremely popular. Its versatility, power, and ease of use led to widespread adoption. And then in October of 1994, Perl 5 was released just as the dot com explosion was getting under way. When you work with the Web, you know web pages are mostly text being sent back and forth. Perl excels at text processing. Because of this, Perl became known as the “duct tape of the Internet” and in an age where merely having “HTML” on your resume guaranteed a job, having Perl next to it meant you were guaranteed an even better paying job.

For many developers, the best strategy for getting a pay raise was to quit every six months and take a new job. Many people who had never programmed before were picking up the basics of Perl and commanding generous salaries. Many Web designers who knew HTML and started playing with this new thing called JavaScript quickly learned that Perl could let them do things with their Web sites that they had never dreamed possible. The rise of the Internet meant the world would never be the same and Perl was driving much of that change.

Perl, in fact, was becoming so popular that it was working its way into our popular culture. The movie Sphere had a bit of Perl (http://www.foo.be/docs/tpj/issues/vol3_4/tpj0304-0013.html), Neal Stephenson included Perl code in the novel Cryptonomicon and Charles Stross, the excellent and now famous science fiction author, wrote a Lovecraftian paean to Perl in issue 4 of The Perl Journal (http://www.foo.be/docs/tpj/issues/vol1_4/tpj0104-0010.html). Truly Perl is the official programming language of the sunken city of R’Lyeh, where dead Cthulhu lies dreaming.

Perl was king, but naturally, competitors arose. Python, first released in 1991, appealed to many due to its simple, easy to learn syntax. Ruby was first released in 1995, but did not become well-known until the release of Ruby on Rails almost a decade later. PHP, originally written in Perl and released in 1995, became a standalone language and is today one of the most popular languages for Web development.

The rise of competition and the love of The Shiny And New meant that Perl’s popularity fell a bit in the 2000s, but it appears to be stabilizing and job site statistics shows that Perl is still one of the most popular dynamic languages around. There is actually a strong job market for Perl and not enough programmers, putting experienced Perl developers in an enviable employment position.

Warning

One problem with Perl’s early success during the dot com era meant that many people who came to Perl had little experience with large-scale programming. Web developers, system administrators and talented amateurs all started developing in Perl and when faced with new challenges often wrote very bad code to overcome these challenges. This has resulted in many companies having a lot of legacy Perl code that can be hard to maintain. Fortunately, Modern Perl is clean and easy to follow (once you understand the language, of course), but the legacy Perl has left a stain on Perl’s reputation.

Note

Perl, Python, Ruby and PHP are all examples of dynamic programming languages. In contrast to languages such as Java, C++ and other static programming languages, the dynamic languages often delay until runtime certain things that static languages might decide at compile time (such as determining which class a method is bound to). Without going into detail well beyond the scope of this book, dynamic languages tend to be rapid to develop in, but have certain kinds of errors that are less common in static languages.

Discussions about dynamic and static typing are about type theory. While the terms are often poorly defined, there are pros and cons with each approach. However, there is one solid rule you should remember: computer scientists have reasonable disagreements about type theory while computer programmers have unreasonable ones. If you get into “static versus dynamic languages” debates and you don’t understand type theory, you’re going to sound like a fool to those who do. So don’t do that.

After Perl 5 was released, there were various releases up to the March 2000 release of Perl 5.6.0, the start of what many think of as the “modern era” for Perl. Amongst other things, it included Unicode and 64-bit support. Table 1-1 summarizes the release dates of many “major” releases of Perl. Note that the format of version numbers changed with the Perl 5.6.0 release.

Table 1.1. Major Perl Releases

Version

Release Date

1.000

December 1987

2.000

June 1988

3.000

October 1989

4.000

March 1991

5.000

October 1994

5.001

March 1995

5.002

February 1996

5.003

June 1996

5.004

May 1997

5.005

July 1998

5.6.0

March 2000

5.8.0

July 2002

5.10.0

December 2007

5.12.0

April 2010

5.14.0

May 2011


As of this writing, Jesse Vincent is overseeing the development of Perl 5.16.0. Perl 6, a new language derived from Perl and a rich variety of other sources, is being actively worked on with several interesting implementations, including a Niecza, which runs on Mono/.NET.

Perl Today

Today Perl is widely used throughout the world. As mentioned, it comes standard on every major operating system apart from Windows. It is still extensively used in Web development, driving many Web sites. New startups are choosing Perl as their language of choice for data processing, system administration, Web development, and other uses. A quick search of many job sites shows plenty of opportunities, but there are fewer competent developers competing for these roles. If a career in Perl interests you, you can also check out http://jobs.perl.org/ for a Web site dedicated to listing jobs that have Perl as their major technology as opposed to jobs where Perl is merely used incidentally.

Given the long history of Perl and the state of Perl today, you might wonder which version of Perl this book covers. This book will mostly focus on 5.8.x and 5.10.x versions of Perl, even though support for both of these has officially been discontinued. Why? This was a difficult decision to make, but there were several reasons for this decision. An important consideration is surveys showing that most businesses are still running these versions of Perl. It’s a strange thing for a book author to stand up and say “this book targets an unsupported version of the language”, but you go to war with the Perl you have, not the Perl you want.

Fortunately, this choice isn’t as bad as it might sound. The Perl 5 Porters (known as “P5P”) work very hard to keep new releases of Perl backwards compatible. Perl 5.14.2 ships with almost half a million tests (455,832 to be exact) to ensure that Perl works exactly as intended. Thus, what you learn to write through this book will generally work unmodified on later versions of Perl.

Getting Perl

Obviously it’s difficult to program Perl if you don’t have it installed on your computer, so we need to cover several methods for doing this. Even if you already have Perl installed, you might want to read this section anyway. If your system depends on your Perl installation, you might want to install a separate version, just to be safe.

System Perl

So how do you get Perl? Well, you’re in luck. Almost every major operating system aside from Windows ships with Perl by default. You can test this by opening up a terminal and typing perl -v at the command line. Currently on my MacBook Pro this prints:

$ perl -v
This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-2level
Copyright 1987-2011, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Currently Perl is supported on over 100 platforms (did you even know there were that many?). If you want a different version of Perl than what you already have installed, check out http://www.perl.org/get.html.

Note

If you are using OS X, you already have Perl installed. However, you will eventually want to build modules or install other modules. To do this you will need to install the Developer Tools found on your OSX install DVD. Only the “UNIX Development Support” tools are required, but there’s no harm (other than disk space) in installing all of them. Why Apple built a wonderful computer for developers and made the development tools optional is one of life’s many inscrutable mysteries.

perlbrew

If you are not running Windows, check out perlbrew (http://www.perlbrew.pl/). This will allow you to run multiple different installations of Perl. This is important because there’s a good chance that some of your operating system depends on the behavior of the system Perl. Therefore, using perlbrew to install your own versions of Perl not only ensures that you don’t have to worry about breaking your system Perl, you also can play with different versions. That being said, so long as you’re not overwriting any modules that your system Perl uses, it’s fine to use your system Perl for learning Perl. It’s also usually fine to upgrade your system modules, but it’s not recommended. If a core module your system depends on changes in an incompatible way, the results are unpredictable. Windows does not have this problem because it does not depend on Perl.

If your system has both bash and curl installed, you can try to install perlbrew with:

curl -kL http://xrl.us/perlbrewinstall | bash

If that works on your system, it should allow you to easily install multiple versions of Perl without superuser (root) permissions. It’s then easy to switch between those versions. This has many benefits, including the following:

  • It’s easy to try new versions of Perl

  • You don’t risk breaking your system’s Perl

  • You don’t need superuser permission to install CPAN modules

  • You can test production code on newer versions of Perl

Once installed, you can type perlbrew help to understand how to use it. It’s pretty easy. To install and use Perl version 5.14.2:

perlbrew install perl-5.14.2
perlbrew switch perl-5.14.2

Note that the installation will take a while because perlbrew needs to download and build the version of Perl you’re asking for.

If you want to install an older version of Perl, you can run:

perlbrew install perl-5.8.3

You can run perlbrew list to see which versions of Perl you have installed and can switch to. Here’s your author’s setup as he’s writing this:

$ perlbrew list
  perl-5.10.1
  perl-5.12.3
  perl-5.14.0
* perl-5.14.2
  perl-5.8.3

The asterisk before the version indicates which version of Perl you’re running at the moment.

And finally, to test your code against different versions of Perl:

perlbrew exec myprogram.pl

Your author has been using this extensively while writing this book. This is extremely useful when you want to find out if your code is compatible with different versions of Perl.

As of this writing, these are the Perl versions available to install (using the perlbrew available command):

$ perlbrew available
  perl-5.15.4
i perl-5.14.2
  perl-5.12.4
i perl-5.10.1
  perl-5.8.9
  perl-5.6.2
  perl5.005_04
  perl5.004_05
  perl5.003_07

The leading i indicates which versions of Perl you have installed and the list of available versions will grow over time.

If you can use perlbrew, it will make your programming life much more pleasant.

Note

Using perlbrew is great, but it requires that you already have Perl 5.8 or better installed on your system. However, as 5.8 was released in July of 2002 (see Table 1-1), this is generally not a problem.

Windows

Windows is one of the few operating systems that does not include Perl by default. This makes things a bit more difficult, but you have a wide variety of options here. Your author recommends Strawberry Perl, but ActivePerl is also an excellent choice. Cygwin is only recommended if you want a Linux emulation layer.

Cygwin

One way to run Perl on Windows is to install Cygwin, a free Linux emulator for Windows. You can download Cygwin from http://www.cygwin.com/. Click on the “Install Cygwin” link for instructions.

By default Cygwin will not install Perl. You can easily find instructions on the Web for installing and running Perl under Cygwin, including many useful YouTube videos. If you go this route, make sure that when you install Cygwin, you select both Perl and the “gcc/C++” packages from “Development” when you’re given a choice on which packages to install. However, to make the most out of Perl on Cygwin, make sure you have the following packages installed:

  • perl

  • gcc/C++

  • gnupg

  • gzip

  • lynx

  • make

  • ncftp

  • ncftpget

  • tar

  • unzip

  • wget

That list should cover most of what you need.

Pros: with Cygwin, you get a Linux environment which means that most Perl programs can run unchanged.

Cons: as an emulation layer, it tends to be a bit slow.

ActiveState Perl

While Cygwin is great, many Windows users are not comfortable working in a Linux type environment. Many Windows developers like ActivePerl from ActiveState (http://www.activestate.com/activeperl). It’s free, but commercial support is provided. ActivePerl has been available for over a decade and is probably the most popular Perl for Windows.

Pros: ActivePerl is has been around for over a decade and maintained by a company with a strong history of supporting Perl and dynamic languages.

Cons: due to the desire to avoid breaking Perl, you cannot update core modules that ship with Perl. Thus, if you are using ppm, their default module installation program (covered in Chapter 2), you cannot install any Perl module that requires on an updated core module.

Strawberry Perl

Strawberry Perl (http://strawberryperl.com) is the newest option for Windows, hence listed last, but it’s the one many prefer today. It’s also free and an excellent alternative, one that Larry Wall himself uses when he’s using Windows. Strawberry Perl does not offer commercial support. Like many open source projects, support is excellent, but on a volunteer basis.

When you install Strawberry Perl, the following software is installed with it:

  • Mingw GCC C/C++ compiler

  • dmake “make” tool

  • ExtUtils::CBuilder and ExtUtils::ParseXS

  • Bundle::CPAN

  • Bundle::LWP (providing more reliable http CPAN repository support)

  • XML::Parser and XML::LibXML, enabling most CPAN XML modules

  • DBI and DBD drivers for SQLite, ODBC, MySQL and Postgres

  • Additional minor modules to enhance the stability of Win32

Don’t worry about what those mean for now. As we move further along through the book, they’ll begin to make sense. Let’s just say that they make Perl on Windows easy enough to use that it rivals Perl on Linux for many tasks. Unless you have a particular reason for using another version of Perl, your author recommends Strawberry Perl.

Pros: Strawberry Perl “just works”. Almost everything you need to develop Perl is bundled with it, including many tools which are pretty much mandatory in a work environment.

Cons: it’s relatively new and companies that rely on Windows are sometimes uncomfortable with software that lacks commercial support.

Note

If you are having issues with getting Perl running on Windows, checkout http://win32.perl.org/. Your easiest (and best) options are to go with the ActiveState or Strawberry Perl options, but win32.perl.org will give you plenty of answers to questions you may encounter.

The Community

You’ll note that you didn’t read detailed instructions on how to install Perl for Windows or how to install alternative versions of Perl on your operating system of choice. As mentioned previously, Perl is supported on over 100 platforms and while your author has tried writing instructions on how to do this in the past, the impossibility of handling that obscure error that someone inevitably has makes this very hard. Fortunately Perl is easy to install on Windows and the language has a very strong community supporting it and they can help you work through even the most unusual issues.

As the Wrox “Programmer to Programmer” series targets experienced developers looking to expand their skills, you’ll likely be familiar with software installation. If you’re new to programming, you might need a bit more help. Either way, here are a variety of resources to get you started.

Note

It’s recommended that you consult these sources regularly when you get stuck on a particular problem. This is one of the lovely things about the open source community: quality help is widely available and it’s free. There’s no need to struggle on your own when so many people are willing to help you learn Perl.

IRC

IRC, or “Internet Relay Chat”, has been around since 1988 and it’s often a great way to get questions answered “in real time”. For Windows, mIRC (http://www.mirc.net/) is probably the most popular IRC client, but it’s shareware and you can only use it for 30 days before paying. KVIrc (http://www.kvirc.net/) is a good, free choice for a graphical IRC client and it’s available for most platforms. For OS X, I use Colloquy (http://colloquy.info/), but really, any IRC client you’re comfortable with is fine. If you use the Firefox browser, it has the capable Chatzilla (http://chatzilla.hacksrus.com/) add-on and this should work regardless of which operating system you choose.

You can also access freenode with any browser via http://webchat.freenode.net/.

Once you get on IRC, connect to the irc.freenode.net server and join #perl. The #perl channel generally has plenty of users and you can get many Perl questions answered quickly and easily ... or at least get told where to RTFM (that stands for “Read The Manual”. The “F” is silent).

If you’re not familiar with IRC, hit you favorite search engine and search for “list of IRC commands”. You can also consult the Wikipedia page for IRC (http://en.wikipedia.org/wiki/Irc) for more information, including lists of other IRC clients.

PerlMonks

PerlMonks (http://www.perlmonks.org/) is a fantastically useful Perl site that’s been around for over a decade. Your author joined back in 2000, unsurprisingly as “Ovid”, and has been a regular contributor for years.

In the top right hand corner of the site you’ll see many useful links. “Seekers of Perl Wisdom” is probably the most useful when you need an answer to a problem you’re facing. When you first post a question, it will show in “Newest Nodes” and many people follow that to try to help answer the new questions. Fortunately, the regular users at PerlMonks generally don’t suffer as much from “first post” silliness you often find at other sites.

In addition to answering questions, PerlMonks has book reviews, “Meditations” (a section for people who just want to muse about Perl-related things), tutorials, Perl news, site discussion, and a “chatterbox” for those who just want casual conversation or have a quick question.

If you’re serious about learning Perl, PerlMonks is a good place to start. Many of the top minds in Perl hang out there and it’s a good resource with plenty of history to search through. PerlMonks is “all Perl, all the time.” Joe Bob says “check it out.”

Perl Mongers

For those who like a bit of real-life interaction (and who doesn’t?), there’s also Perl Mongers (http://www.pm.org/). Founded by brian d foy in 1997, Perl Mongers is an organization of Perl hackers in different cities who meet periodically to, well, do whatever they want. Your author ran the Perl Mongers group in Portland, Oregon (Portland.pm) for several years, and has attended Perl Mongers meetings in a number of countries.

The local Perl Mongers user groups are Perl enthusiasts who enjoy hanging out together and talking about stuff. Sometimes that stuff is Perl. The Portland.pm group generally schedules technical talks followed by a “social” at a local pub, often the excellent Lucky Lab in Portland, Oregon. If you ever visit Portland, check out that pub.

There are Perl Mongers groups on every continent except Antarctica, but there was discussion of that when we found out there was a Perl programmer there. If you live near a major city, there’s a good chance there’s a Perl Mongers group close to you. If not, create one!

StackOverflow

StackOverflow (http://stackoverflow.com/) was created in 2008 by Joel Spolsky and Jeff Atwood as an “open” site for anyone to ask programming related questions. It has spun off numerous related sites and has become extremely popular as the site to ask just about any technology question that comes to mind. Perl questions are answered quickly with solid information and you can easily see the “rating” of the users who respond to your questions. Because of the nature how questions are tagged, it’s easy to quickly drill down to questions that might be relevant to your situation.

Warning

Learn how to ask effective questions!

Quite often on PerlMonks, StackOverflow or other sites, you’ll see a question like “I tried to use module XYZ, but when I tried to print with it, it didn’t work. What am I doing wrong?”

That’s it. “didn’t work” isn’t explained. No code sample is provided. Nothing.

Here’s how to ask an affective question:

  1. State what you’re trying to do.

  2. Explain how you tried to do it.

  3. Explain what result you expected.

  4. Explain what result you had instead.

“How you tried to do it” often involves posting a minimal code sample. Posting no code is just as bad as posting 500 lines of code. Just give people an idea of what you’re trying to do and answer any follow-up questions they have, if any.

It’s also a good idea to indicate how you already tried to find an answer. People are often more helpful if it looks like you’ve already tried to find an answer to a basic question.

Perldoc

Now that you’ve installed Perl, the first thing you should do is get acquainted with the extensive Perl documentation that ships with the language. As I cover various topics in this book, I’ll often prefix sections with a perldoc tip like this:

perldoc perlnumber

Typing perldoc perlnumber into your terminal will give you an introduction to how numbers are used in Perl. If you prefer a Web browser, go to http://perldoc.perl.org/, select your Perl version and then go to:

http://perldoc.perl.org/perlnumber.html

By constantly reinforcing perldoc throughout this text, I’ll give you the tools to find answers to most questions yourself. This is one bit of advice I wish I had received when I started my Perl journey back in the 90s. In fact, you don’t need to memorize the material in the documentation, but as you become more familiar with it, it will be will easier to remember where to look it up later.

The Structure of perldoc

The Perl documentation is written in POD, short for “Plain Old Documentation”. POD is a very easy to learn markup language for documenting Perl. In fact, it’s easy enough to learn (and you will in Chapter 11, Packages and Modules), but flexible enough, that many authors write their books in POD (I almost did, but it’s a long story why not).

When you type perldoc <documentation name>, the program searches through parts of your system where it thinks the documentation may be found, looking for a file with a .pod or .pm extension (the former is preferred, the latter is used if it has embedded POD and the former is not found). It then formats the POD and displays it to you. For earlier versions of perldoc, you could add the -v switch to see where it is looking for your POD:

perldoc -v perldoc

If your version of perldoc supports this, use the -D switch to see where perldoc is looking for the documentation. The -v switch is now used to display the description of Perl’s built-in variables:

perldoc -v '$_'
perldoc -v '@ARGV'

(Or you can just type perldoc perlvar to see all of Perl’s built-in variables).

You can read perldoc perldoc for more information about how to customize its output or to see what other command line switches it supports.

Getting Started with perldoc

The first thing you want to do is type perldoc perl. This will give you a very brief description of some of what Perl can do and it quickly directs you to:

perldoc perlintro

That’s a gentle introduction to Perl. If you’re really dedicated, you could start there and not buy this or any other Perl book. That approach works if you have lots of time and patience. This book presents what you need to know most, including where to get more.

Note that the perlintro is clear, but very terse. It assumes that you already know how to program and rushes through the basic features of the language. As a result, there are many bits and pieces you should be aware of, but won’t be. So to follow up on the perlintro, you’ll want:

perldoc perltoc

As you might expect, that’s the “Table of Contents” for the Perl documentation. For Perl 5.14.2, that “Table of Contents” is over 20,000 lines long! That’s a huge amount of documentation. It’s longer than many of the chapters in this book and your author hopes his publisher doesn’t notice. In contrast, Perl 5.8.3’s table of contents weighs in at a measly 11,911 lines. However, since we’ll mostly be focusing on 5.8 and 5.10, we won’t really talk (much) about what’s in those newer documents.

Tutorials and FAQs

Perl comes bundled with many tutorials you can read with perldoc. The tutorials are listed in Table 1-2 are some of the popular tutorials included in Perl version 5.8.3.

Table 1.2. Perldoc Tutorials

Tutorial

Description

perlreftut

Tutorial on references

perldsc

Data structures cookbook

perllol

Data structures: arrays of arrays

perlrequick

Regular expression quickstart

perlretut

Regular expression tutorial

perlboot

Object Oriented (OO) Perl for beginners

perltoot

OO tutorial, part 1

perltooc

OO tutorial, part 2

perlbot

OO tricks and examples

perlstyle

Style guide

perlcheat

Cheat sheet

perltrap

Traps for the unwary

perldebtut

Debugger Tutorial


You can type perldoc <tutorialname> to read these tutorials.

Since I’m having so much fun cutting and pasting from the documentation and padding my page count, Table 1-3 lists the FAQs (Frequently Asked Questions) that ship with Perl:

Table 1.3. perlfaq

FAQ

Description

perlfaq

Perl frequently asked questions

perlfaq1

General questions about Perl

perlfaq2

Obtaining and learning about Perl

perlfaq3

Programming tools

perlfaq4

Data manipulation

perlfaq5

Files and formats

perlfaq6

Regexes (regular expressions)

perlfaq7

Perl language issues

perlfaq8

System interaction

perlfaq9

Networking


These FAQs are extensive. For example, here are some of the questions addressed in perlfaq2:

  • What machines support Perl? Where do I get it?

  • How can I get a binary version of Perl?

  • I don’t have a C compiler on my system. How can I compile perl?

  • I copied the Perl binary from one machine to another, but scripts don’t work.

What’s really nice is that for any of these questions, you can type perldoc -q "something I'm looking for" and perldoc will spit out the sections from any FAQ which match the term you give it (actually, perldoc will match against what’s called a “regular expression”, but we’re not covering those until Chapter 8, Regular Expressions, so pretend you didn’t read that bit).

We also have a full reference manual shipped with the Perl documentation along with extensive information about the internals of Perl (not for the faint of heart), linking Perl to C and C++ programs, platform specific information, and other things we won’t cover in this book.

perldoc -f function

One of the most useful perldoc commands is perldoc -f, followed by a function name, you can see a complete description of the function in question and quite possibly far more than you will ever need to know. For example, perldoc -f my displays the following:

my EXPR
my TYPE EXPR
my EXPR : ATTRS
my TYPE EXPR : ATTRS
        A "my" declares the listed variables to be local (lexically) to
        the enclosing block, file, or "eval".  If more than one value
        is listed, the list must be placed in parentheses.
        The exact semantics and interface of TYPE and ATTRS are still
        evolving.  TYPE is currently bound to the use of the "fields"
        pragma, and attributes are handled using the "attributes"
        pragma, or starting from Perl 5.8.0 also via the
        "Attribute::Handlers" module.  See "Private Variables via my()"
        in perlsub for details, and fields, attributes, and
        Attribute::Handlers.

It starts with the grammar for the function and then a brief (and sometimes very verbose) explanation of that function. In the example above, the grammar could represent any of the following:

my $dog;
my Dog $spot;
my $dog : HasSpots;
my Dog $spot : HasSpots;

Note

In real-world Perl, you’ll almost always see the first form, my $dog, and not the following three. Their semantics were never well-defined and caused confusion, so people don’t use them. That’s an example where the docs show you what you can do, not what you should do.

Using a terminal

You can skip this section if you already know how to use a terminal window.

The Command Line

Perl comes from a Unix background and as a result, is often run from a terminal window. Unlike many GUI (Graphical User Interface) systems, terminals allow you to type commands directly into the system rather than clicking an icon on a screen or selecting items from menus. Getting used to the command line will eventually give you all of the power of a GUI system, but also leverage the considerable power of the command line. If you’re not familiar with it, you’ll want to hit your favorite search engine for how to use the command line on your system, but for now, we’ll just concentrate on getting a terminal window open.

This isn’t very hard, but ask a geek friend for help if you get stuck.

Linux

If you’re familiar with Linux, you probably already know about the terminal window. Unfortunately, because there are well over 100 Linux distributions and many different window managers, it’s impossible for me to tell you how to do this on your system. However, there are some general tips.

First, look for an icon on your desktop that looks like a computer screen. It may say Terminal or Console next to it. Try double-clicking that. You can also often right-click on your desktop and look for open terminal or something similar. In the menu system under the System folder you may also find the Konsole or Gnome Terminal program.

Finally, you can search your desktop menu for the words “terminal” or “console”. Many Linux systems have icons on their menus and you may see a terminal icon there.

Mac OS X

If you’re on a Mac, go to your desktop and type Command-Shift-G (in other words, hit all of those keys at the same time). This will bring up a “Go to folder” dialog. Type /Applications/Utilities in the text window and click “Go”. Scroll through the applications until you see the Terminal icon. Drag this to the dock. You’ll be using the terminal a lot in this book, so you want to have this handy. A quick check of a search engine for “mac command line” or “learning os x terminal” should bring you up to speed on some of the basic commands. When you’re using the Mac command line, you’ll find that most stand Unix/Linux commands operate the same way.

As an alternative, checkout out iTerm2 (http://www.iterm2.com/), a free replacement for the Terminal application. Your author uses iTerm2 and finds it to be an excellent replacement for Terminal.

Windows

For Windows, you have a couple of options. If you’ve installed Cygwin, you can double-click on the Cygwin desktop icon (not the installer!) and you’ll automatically be at a command line prompt ready to go. Otherwise, type the “Windows” key and “r” at the same time. This should bring up a “Run” dialog box. Type cmd (short for “command”) into the box, click “ok”, and a terminal window will pop up.

You can also bring up the “Run” dialog box by clicking “Start” and then you should see “Run” as one of the menu items. Click on that and it’s the same procedure: type cmd into that box and click “ok”.

If you don’t like the standard terminal on Windows, some people prefer console, available via free download at http://sourceforge.net/projects/console/.

For Windows, the terminal Window is sometimes referred to as a DOS window. DOS stands for “Disk Operating System” and earlier versions of Windows were based on DOS with a Window manager on top. Today, Windows is a GUI system and the DOS window is an emulation layer, but the commands have not changed much over time.

If you’re unfamiliar with the Windows command line, search the Internet for “list of DOS commands” to learn more about this environment.

Creating a Work Directory

Now that you have a terminal window open, you might want to have a clue where you are on your system. To see the current directory you are in, you can type pwd (print working directory) on Linux or OS X, or cd (with no arguments) on Windows. You can type ls on Linux or OS X to see a list of files in the current directory or dir if you’re on Windows.

Note

A “folder” in Windows or OS X is what most other operating systems refer to as a “directory”. We’ll say “directory” throughout this text.

You’ll want to create a folder named wroxperl and change to it. For all operating systems, you should be able to type this:

mkdir wroxperl
cd wroxperl

You should now be in an empty directory, suitable for creating your sample programs. I recommend that when you create them, you make them in separate directories named chapter1, chapter2, and so on. This will make it easier to organize and refer back to them. So go ahead and create a chapter1 directory now and change to it:

mkdir chapter1
cd chapter1

You won’t need this until we get to our “Hello, World!” program (it’s a law that all programming books have to start with this), but stay in the terminal for now because you’re going to get used to the perldoc command.

Note

You probably have perldoc installed. Annoyingly enough, some systems that include Perl by default don’t include the perldoc command even though it is installed by default when you install Perl manually. If your system uses apt, you can install perldoc with:

sudo apt-get install perl-doc

Unfortunately, that won’t work on systems which don’t use apt and because Perl is available on over 100 platforms, it’s impossible for this book to cover them all. Thus, try hitting IRC, PerlMonks, StackOverflow or your favorite search engine to find out how to install perldoc. Or ask your geek friend to do it for you. Pizza is a great payment.

Hello, World!

Now that you’ve read far too much documentation (who am I kidding? You skimmed it), it is time for that traditional rite (write?) of passage, “Hello, World!” As one friend explained to me, he was proud that he could write “Hello, World!” in 15 programming languages, though he could program in none. Let’s try to avoid that, shall we?

Your First Program

First, open your terminal and type this:

perl -e 'print "Hello, Wrox!\n"'

Oh, wait. Sorry Windows people. You have to type this:

perl -e "print \"Hello, Wrox!\n\""

Except that it might not work, depending on your version of Windows. See perldoc perlfaq3 and read the section entitled “Why don’t Perl one-liners work on my DOS/Mac/VMS system?” to understand why your life is more difficult. If you have a Mac, the “Mac” section likely will not apply to you as OS X handles Perl and the command line quite well, thank you.

Aside from my blatant brown-nosing of my publisher, the “Hello, Wrox!” snippet shows something very common about Perl: running Perl from the command line. This won’t be covered much this much in the book, but as you get more familiar with Perl, you’ll see people doing things like this:

perl -pi.bak -e 's/version = 13/version = 14/' <list of files>

That will change all strings in <list of files> matching “version = 13” to “version = 14” and create backups of all of those files with a .bak extension. That’s more or less equivalent to this Perl program that is also listed in perldoc perlrun (though we’ve cleaned it up to be “safer”).

#!/usr/bin/perl
my $extension = '.bak';
my $oldargv;
LINE: while (<>) {
    if ($ARGV ne $oldargv) {
        my $backup;
        if ($extension !~ /\*/) {
            $backup = $ARGV . $extension;
        }
        else {
            ($backup = $extension) =~ s/\*/$ARGV/g;
        }
        rename($ARGV, $backup);
        open(ARGVOUT, ">$ARGV");
        select(ARGVOUT);
        $oldargv = $ARGV;
    }
    s/version = 13/version = 14/;
}
continue {
    print;  # this prints to original filename
}
select(STDOUT);

As you can see, using Perl on the command line effectively can give you a lot of power to get things done quickly. You can read perldoc perlrun to understand some of this, but you should search for “perl one-liners” online to see what you can do if you’re interested in this area.

Getting back to “Hello, World!”, the general way you write a Perl program is to save a file with the program code and then type perl <programname>. The first line of the program is often the shebang line, which we’ll explain a more about in a bit. After that is your program text. All you need to do to get a basic Perl program running is to type up your program, save it (usually with a .pl extension), and then type perl programname. Listing 1-1 is a short Perl program that shows how a simple program may look. We will explain the strict, warnings and diagnostics in Chapter 3, Variables.

Example 1.1. Hello, world!

#!perl
use strict;
use warnings;
use diagnostics;
# this is a comment
print "Hello, World!\n"; # so is this

Shebang lines

The first line of a Perl program often starts with a shebang line. A shebang line starts with “sharp” (#) and an exclamation point, also known as a “bang” (!), hence the term “shebang”. It is followed by a path telling the shell where to find the interpreter. On a system which understands the chmod command, you can type chmod +x programname to make the program directly executable. If it’s in your path, you can then type programname to run the program. Otherwise, you can type the full or relative path to the program to execute it. For example, if you’re in /Users/ovid/wroxperl/chapter1 and you create a program called runme in that directory, you could run it like this:

$ ./runme
$ /Users/ovid/wroxperl/chapter1/runme

For now, you can just type perl programname to run the programs.

The shebang line might take one of a number of different forms. On a Linux system, this will often look like one of the following:

#!/usr/bin/perl
#!/usr/local/bin/perl
#!/usr/bin/perl -w
#!/usr/bin/env perl

The first two versions point directly to the Perl executable which should run the program. The third version, with the -w switch, tells Perl to run the program with global warnings. The final version tells Perl to use the env program to find out which perl is currently set as the default perl for your system.

Some people just do the following:

#!/perl

And that will generally do what you want.

On Windows you might see the following:

#!C:\Perl\bin\perl.exe
#!C:\strawberry\perl\bin\perl.exe

The first version is often found when running with ActiveState Perl. The second version is found when running with Strawberry Perl.

When perl sees the shebang line, it will attempt to run your program using whatever it finds after the #!. Generally this will not be a problem, but if you find that you want to run the script on more than one machine, even with the same architecture, you could have a problem if someone installs perl in a different location.

Fortunately, there is one simple trick you can follow to ensure you don’t have problems with shebang lines: don’t install modules and scripts by hand. Instead, you package them as proper distributions and install them with the standard Perl toolchain (such as cpan or cpanm). You will learn module installation in Chapter 2 and module writing in Chapter 11, Packages and Modules.

Summary

By this time you’ve learned a bit about the history of Perl, where to go to get more information, installing Perl and running a simple Perl program. This hasn’t been a huge amount of information, but it’s the foundation you need to really progress in Perl.

What You Learned in This Chapter

Topic

Key Concepts

History

The basic history of the Perl language, its releases and common use.

Getting Perl

You learned about system Perl and perlbrew for those who use Unix-style systems. Cygwin, ActivePerl and Strawberry Perl are compared as options for Windows users.

Community

Perlmonks, IRC, Perl Mongers and StackOverflow are all valuable resources for learning Perl.

Perldoc

Perl comes with extensive documentation. You learned the basic structure of the docs and how to look up basic information.

Using a terminal

A terminal is used extensively when programming Perl. You learned how to launch a terminal and run a program from the command line.

Site last updated on: July 5, 2012 at 11:41:08 AM PDT
Cover for Beginning Perl (Wrox)

View 1 comment

  1. Ben Bullock – Posted June 15, 2012

    "including a Niecza" -> "including Niecza" ?

Add a comment

View 1 comment

  1. dawpa2000 – Posted June 15, 2012

    Add comma after "Today" in "Today Perl is widely used throughout the world."

Add a comment

View 1 comment

  1. brianwisti – Posted June 18, 2012

    You may have already updated this in your latest draft, but it should probably mentioned that Developer Tools is available from the app store instead of a DVD for 10.7. Otherwise they'll be trying to find a Developer disk and thinking they don't have the full OS. Which ... well ... that is one interpretation of the change.

Add a comment

View 2 comments

  1. m@rtin – Posted June 14, 2012

    I would not recommend feeding the output of some HTTP query directly to a command interpreter. This is very insecure. -Martin

    The author has indicated that the issue raised in this comment has been resolved.

  2. Curtis Poe – Posted June 14, 2012

    m@rtin: In this case, it's the documented way of installing perlbrew and it's created by devs I have complete trust in. That being said, that's a very good point and I should note that. Many programming languages have installation toolchains from "untrusted" sources that are extremely insecure, but tend not to be exploited. I'm not sure of what the best option here is.

    The author has indicated that the issue raised in this comment has been resolved.

Add a comment

View 1 comment

  1. iang – Posted Oct. 19, 2012

    s/affective/effective

Add a comment

View 1 comment

  1. mfisher911 – Posted June 14, 2012

    I think it's easier to use Spotlight (the magnifying glass in the top right corner or Cmd-Space) to search for "Terminal". The Terminal app can be dragged to the Dock from the Spotlight results.

    The author has indicated that the issue raised in this comment has been resolved.

Add a comment

View 1 comment

  1. iang – Posted Oct. 19, 2012

    s/you can run the this command/you can run this command

Add a comment

View 2 comments

  1. chrisjack1 – Posted June 27, 2012

    Wow - you're hitting your readers with this in the first chapter of a "Beginning Perl" book...

  2. Luciano Rocha – Posted Aug. 22, 2012

    That probably won't work on Windows, or other operating system that doesn't allow you to rename a file that is opened.

Add a comment

View 1 comment

  1. chrisjack1 – Posted June 27, 2012

    You might mention: which perl can give you a clue about where at least one version of perl might reside.

Add a comment