Perl Programming Documentation
Perl 5 version 26.1 documentation
perlintro -- a brief introduction and overview of Perl
This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other peoples' Perl and understand roughly what it's doing, or write your own simple scripts.
This introductory document does not aim to be complete. It does not even aim to be entirely accurate. In some cases perfection has been sacrificed in the goal of getting the general idea across. You are strongly advised to follow this introduction with more information from the full Perl manual, the table of contents to which can be found in perltoc.
Throughout this document you'll see references to other parts of the
Perl documentation. You can read that documentation using the
Throughout Perl's documentation, you'll find numerous examples intended to help explain the discussed features. Please keep in mind that many of them are code fragments rather than complete programs.
These examples often reflect the style and preference of the author of
that piece of the documentation, and may be briefer than a corresponding
line of code in a real program. Except where otherwise noted, you
should assume that
Do note that the examples have been written by many different authors over a period of several decades. Styles and techniques will therefore differ, although some effort has been made to not vary styles too widely in the same sections. Do not consider one style to be better than others - "There's More Than One Way To Do It" is one of Perl's mottos. After all, in your journey as a programmer, you are likely to encounter different styles.
What is Perl?
Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.
The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that it's easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the world's most impressive collections of third-party modules.
Different definitions of Perl are given in perl, perlfaq1 and no doubt other places. From this we can determine that Perl is different things to different people, but that lots of people think it's at least worth writing about.
Running Perl programs
To run a Perl program from the Unix command line:
Alternatively, put this as the first line of your script:
... and run the script as /path/to/script.pl. Of course, it'll need
to be executable first, so
(This start line assumes you have the env program. You can also put
directly the path to your perl executable, like in
For more information, including instructions for other platforms such as Windows and Mac OS, read perlrun.
Perl by default is very forgiving. In order to make it more robust it is recommended to start every program with the following lines:
The two additional lines request from perl to catch various common
problems in your code. They check different things so you need both. A
potential problem caught by
Basic syntax overview
A Perl script or program consists of one or more statements. These
statements are simply written in the script in a straightforward
fashion. There is no need to have a
Perl statements end in a semi-colon:
Comments start with a hash symbol and run to the end of the line
Whitespace is irrelevant:
... except inside quoted strings:
Double quotes or single quotes may be used around literal strings:
However, only double quotes "interpolate" variables and special
characters such as newlines (
Numbers don't need quotes around them:
You can use parentheses for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify issues of precedence.
More detailed information about Perl syntax can be found in perlsyn.
Perl variable types
Perl has three main variable types: scalars, arrays, and hashes.
Scalars, arrays and hashes are documented more fully in perldata.
More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes.
A reference is a scalar value and can refer to any other Perl data type. So by storing a reference as the value of an array or hash element, you can easily create lists and hashes within lists and hashes. The following example shows a 2 level hash of hash structure using anonymous hash references.
Throughout the previous section all the examples have used the syntax:
However, the above usage will create global variables throughout your
program, which is bad programming practice.
Conditional and looping constructs
Perl has most of the usual conditional and looping constructs. As of Perl
5.10, it even has a case/switch statement (spelled
The conditions can be any Perl expression. See the list of operators in the next section for information on comparison and boolean logic operators, which are commonly used in conditional statements.
For more detail on looping constructs (and some that weren't mentioned in this overview) see perlsyn.
Builtin operators and functions
Perl comes with a wide selection of builtin functions. Some of the ones
we've already seen include
Perl operators are documented in full in perlop, but here are a few of the most common ones:
Many operators can be combined with a
Files and I/O
You can read from an open filehandle using the
Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog. Most text file processing can be done a line at a time with Perl's looping constructs.
When you're done with your filehandles, you should
Writing subroutines is easy:
Now we can use the subroutine just as any other built-in function:
We can manipulate
Subroutines can also return values:
Then use it like:
For more information on writing subroutines, see perlsub.
OO Perl is relatively simple and is implemented using references which know what sort of object they are based on Perl's concept of packages. However, OO Perl is largely beyond the scope of this document. Read perlootut and perlobj.
As a beginning Perl programmer, your most common use of OO Perl will be in using third-party modules, which are documented below.
Using Perl modules
Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself.
Categories of modules range from text manipulation to network protocols to database integration to graphics. A categorized list of modules is also available from CPAN.
To learn how to install modules you download from CPAN, read perlmodinstall.
To learn how to use a particular module, use
perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use.
If you feel the urge to write Perl modules, perlnewmod will give you good advice.
Kirrily "Skud" Robert <email@example.com>