Perl is a powerful scripting language. Image by RapidEye / Getty ImagesPerl is a relatively simple, popular, and highly regarded scripting language. It serves many purposes, such as generating DOS batch files or C shell scripts. In web development, it’s particularly useful for creating CGI scripts.
A great advantage of Perl is that, being a scripting language, it allows for sharing source code of programs. This gives you the chance to learn through examples, and you can download and adapt thousands of Perl scripts for your own projects. However, a drawback is that much of this free code is extremely difficult to decipher, as Perl often uses a notoriously cryptic style!
This article assumes you're already familiar with programming (if you're comfortable with the C programming language, you'll find this especially easy). Perl is simple to use once you grasp the basics. We will start from the very beginning and guide you through the most common programming tasks with Perl. By the end, you'll be able to write your own Perl scripts with ease, and although reading cryptic scripts written by others may still be tricky, you'll have a solid foundation to build from.
Getting Started
To begin with Perl, you will need the Perl interpreter. On most UNIX systems, it's almost certainly already installed. For Windows or Mac, you'll need to download and install the latest version of Perl (see the links at the end of this article for more details). Perl is freely available and can be found on many websites.
Next, check out the DOCS directory that comes with Perl -- it contains user manuals and other helpful resources. It’s a good idea to skim through the documentation, even if it feels overwhelming at first. After reading this article, you'll find it much easier to understand.
Hello World
Once you’ve installed Perl, ensure your system’s path is correctly set to include the Perl executable. Then, open a text editor and create a new text file. Inside the file, enter the following line:
print "Hello World!\n";
Save the file as "test1.pl". Then, at the command prompt, type:
perl test1.pl
Perl will process and run the code in your file. You should see the output "Hello World!" displayed on the screen (stdout). As you can tell, running Perl programs is a breeze. (If you're using UNIX, you can include a comment like #! /usr/bin/perl at the start of the file, so you don’t need to type "perl" every time at the command line.)
The print command outputs text to stdout. The \n symbol represents a line break. This will become clearer if you modify the test program as follows (# indicates a comment):
# Print on two lines print "Hello\nWorld!\n";
Notice that the print command recognized the "\n" as a line break instead of the actual characters. This interpretation happens not due to the print command itself, but because of the use of double quotes (a technique called quoting in Perl). If you were to use single quotes instead, like this:
print 'Hello\nWorld!\n';
In this case, the \n character would not be interpreted and would remain as the literal text.
Another useful character is the backquote: `. When you use a pair of backquotes, anything inside them is treated as an operating system command, which gets executed, and the output of that command is printed. For example, if you place an operating system command inside the backquotes, it will run. On Windows NT, for instance, you can execute:
print `cmd /c dir`;
This will run the DIR command and display a list of files from the current directory.
You will also encounter the / character, which is used for quoting regular expressions.
The print command recognizes commas as separators. For instance:
print 'hello', "\n", 'world!';
You might also encounter the period character:
print 'hello'. "\n". 'world!';
The period here acts as a string concatenation operator.
Additionally, for those familiar with C, there's also a printf operator.
PERL Note
In Windows NT, you can't simply use:
print `dir`;
This is because 'dir' isn't a standalone executable; it’s part of the command interpreter (cmd). For more details, type cmd /? at a DOS prompt.
Variables
In Perl, variables are unique. You don't need to declare them, and they are always prefixed with a $. They are created when they are first used. For example:
$s = "Hello\nWorld\n"; $t = 'Hello\nWorld\n'; print $s, "\n", $t;
Alternatively:
$i = 5; $j = $i + 5; print $i, "\t", $i + 1, "\t", $j; # \t = tab
Another example:
$a = "Hello "; $b = "World\n"; $c = $a . $b; # notice the use of . for string concatenation print $c;
Since . is used for concatenating strings, .= behaves similarly to "+=" in C. So, you can write:
$a = "Hello "; $b = "World\n"; $a .= $b; print $a;
You can also define arrays:
@a = ('cat', 'dog', 'eel');
print @a, "\n";
print $#a, "\n"; # Shows the highest index, zero-based
print $a[0], "\n";
print $a[0], $a[1], $a[2], "\n";The $# syntax returns the highest index in the array, which is equivalent to the total number of elements minus one. Similar to C, arrays in Perl begin indexing at zero.
You can also define hashes:
%h = ('dog', 'bark', 'cat', 'meow', 'eel', 'zap');
print "The dog says ", $h{'dog'};In this case, 'bark' is paired with 'dog', 'meow' with 'cat', and so on. A more readable version of the same definition looks like this:
%h = (
dog => 'bark',
cat => 'meow',
eel => 'zap'
);The => operator serves to quote the left-hand string and functions similarly to a comma.
Loops and Ifs
To create a basic for loop, you can follow the syntax similar to C:
for ($i = 0; $i < 10; $i++)
{
print $i, "\n";
}While loops are straightforward as well:
$i = 0;
while ( $i < 10 )
{
print $i, "\n";
$i++;
}If conditions are just as simple:
for ($i = 0; $i < 10; $i++)
{
if ($i != 5)
{
print $i, "\n";
}
}The boolean operators function similarly to how they do in C:
- && for "and"
- || for "or"
- ! for "not"
For numbers:
- == for equality
- != for inequality
- <, <=, >, >= for comparison (as expected)
For other comparisons:
- eq for string equality
- ne for string inequality
- lt for less than
- le for less than or equal
- gt for greater than
- ge for greater than or equal
To loop through an array, you can use foreach:
@a = ('dog', 'cat', 'eel');
foreach $b (@a)
{
print $b, "\n";
}The foreach loop iterates over each element in the array @a, placing it in $b until all elements are processed.
PERL Note
Even for a single line of code, you must enclose the block within braces ({ and }).
Functions
To define a subroutine in PERL, use the keyword sub. Any arguments passed to the subroutine will be contained within an array called _. The following example demonstrates this:
show ('cat', 'dog', 'eel');
sub show
{
for ($i = 0; $i <= $#_; $i++)
{
print $_[$i], "\n";
}
}Keep in mind that $# returns the highest index of an array, which is one less than the number of elements. Therefore, $#_ gives you the count of parameters minus one. If you enjoy this kind of cryptic style, PERL might be to your liking.
In a subroutine, you can declare local variables using the keyword local, like so:
sub xxx
{
local ($a, $b, $c)
...
}You can invoke a function using the & symbol, like so:
&show ('a', 'b', 'c');The & symbol is typically needed only when there is ambiguity, but many developers choose to use it consistently for clarity.
To return a value from a subroutine, the keyword return is used.
Reading
Reading input from STDIN
To capture input from standard input (stdin), utilize the STDIN handle. Here's an example:
print "Enter high number: "; $i =; for ($j = 0; $j <= $i; $j++) { print $j, "\n"; }
As long as you input an integer, this program will run correctly. The <STDIN> reads one line at a time. Additionally, you can use getc to read a single character as shown below:
$i = getc(STDIN);
Alternatively, you can use the read function:
read(STDIN, $i, 1);
The 1 in the third parameter of the read function specifies the number of characters to read.
Accessing Environment Variables
PERL provides a global hash called ENV that allows you to retrieve environment variable values. For example:
print $ENV{'PATH'};Handling Command Line Inputs
In PERL, the global array ARGV stores command line arguments provided to the script. The value of $#ARGV represents the total count of arguments minus one, $ARGV[0] is the first argument, $ARGV[1] is the second, and so forth.
Environment variable names must be in uppercase.
