Learning Perl for Beginners: Getting Started

Last updated: Sep 22, 2008

Well if you have decided to take the plunge into Perl I commend you. So far my experience with Perl has been really great. I have already made very useful programs for both Windows and Linux. Coming from a web development background I like to think of Perl as the “PHP for operating systems”. If PHP is great for creating dynamic websites, Perl is great for manipulating anything to do with your operating system and applications. The best part is Perl has similar syntax.

Before you can create all this useful stuff you need to actually go out and get the Perl interpreter since it is a scripting language.

Depending on your operating system the installation will vary.

Windows

The most common way to get Perl on a Windows machine is to download and install ActiveState Perl.

The default install options are fine.

Linux

If you have a modern Linux distribution you probably already have Perl. Go ahead and type ‘perl -v’ at the command line and see if you get something.

If you get the version number great otherwise you will need to download and install it with your distro’s package management system.

On a Debian/Ubuntu system you would do:

apt-get install perl

Do I need an IDE?

After installing Perl you will probably need something to actually write the code. I suggest using Vim/vi since it will save you a tremendous amount of time. If you haven’t already learned it you should start right away.

If you feel like you have too much to learn right now as it is, you can go ahead and use notepad or your favorite text editor. With Perl there really isn’t a need to use an IDE, a simple text editor will work.

Writing Your First Perl Program

Traditionally your first program is a “Hello World”. So that’s what we will make first.

Open up your text editor and lets start.

The first part of a Perl program tells Perl where to find the interpreter. This line is the first line in your code and might look like this:

#!/usr/bin/perl

This tells the OS to look in the directory, ‘/usr/bin/perl’ to find the interpreter, Although this is a Linux path, Windows users using ActivePerl can still use this.

Why do I need this?

The reason for this is that Perl was originally written for Linux and Linux doesn’t rely on the extension type (the last three characters of a file name after the dot) to determine which program will open it. Linux will look into the script and determine the program based on the first line. This is why bash scripts will have, ‘#!/bin/bash’ at the beginning. This allows you to name your program, “MyProgram” and not, “MyProgram.xyz”.

The next thing we need to do is display “Hello World”. Add this to your code:

print 'Hello World';

Look familiar? It should. So far this code would be the exact same code you would use to print a Hello World type program in PHP. The only difference is that you might use the command echo instead of print.

Like PHP most Perl functions end in a semicolon.

In this example we used single quotes to hold our string that we want to print. We could have easily used double quotes like this:

print "Hello World";

One important difference between double quotes and single is that double quoted strings will look for special functions within the string. This works the same way in PHP.

For example:

print "Hello World\n";

Would print the text, ‘Hello World’ and then a blank line or new line.

Double quoted text will also look for variables.

$myVariable = 'Hello World'; print "Perl says $myVariable";

This would display, ‘Perl says Hello World’. Like PHP variable names almost always have a ($) dollar sign in front of them.

Putting it all together

Your code for a simple Hello World program will look like this:

#!/usr/bin/perl print 'Hello World!';

Save your program as, helloWorld.pl

To run your program navigate to the directory and type, ‘perl helloWorld.pl’. You should get a display that says, Hello World!”

You just finished your first Perl program. Although your program doesn’t really do much we will quickly progress to something that is useful in future posts.

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Get notified on new posts or other things I'm working on

Share: