How to Install Perl and Run Your First Script

01
of 04

How to Install Perl and Run Your First Script

 

So you’re ready to take those first tentative steps into the fascinating world of Perl.  You need to set up Perl on your computer and then write your first script.

The first thing most programmers learn how to do in a new language is to instruct their computer to print a "Hello, World" message to the screen. It's traditional. You'll learn to do something similar but slightly more advanced to show just how easy it is to get up and running with Perl.

02
of 04

Check if Perl Is Installed

Before you download Perl, you should check to see if you already have it. Many applications use Perl in one form or another, so it may have been included when you installed an application. Macs ship with Perl installed. Linux probably has it installed. Windows ​doesn't install Perl by default.

It's easy enough to check. Just open a command prompt (in Windows, just type cmd in the run dialog and press Enter. If you're on a Mac or on Linux, open a terminal window).

At the prompt type:

perl-v

and press Enter. If Perl is installed, you receive a message indicating its version.

If you get an error such as "Bad command or file name," you need to install Perl. 

03
of 04

Download and Install Perl

If Perl is not already installed, download the installer and install it yourself.

Download the Perl Installer

Close the command prompt or terminal session. Go to the Perl download page and click on the Download ActivePerl link for your operating system. 

ActivePerl vs. Strawberry Perl

If you are on Windows, you may see a choice of ActivePerl and Strawberry Perl. If you're a beginner, choose ActivePerl. If you have experience with Perl, you may decide to go with Strawberry Perl.  The versions are similar, so it's entirely up to you.

Run the Perl Installer

Follow the links to download the installer and then run it. Accept all the defaults and after a few minutes, Perl is installed. Check by opening up the command prompt/terminal session window and repeating the

perl -v

command.

You should see a message indicating you have installed Perl correctly and are ready to write your first script.

04
of 04

Write And Run Your First Script

Use Your Favorite Text Editor

All you need to write Perl programs is a text editor. Notepad, TextEdit, Vi, Emacs, Textmate, Ultra Edit and many other text editors can handle the job.

Just make sure you're not using a word processor like Microsoft Word or OpenOffice Writer. Word processors store text along with special formatting codes that can confuse programming languages.

Write Your Script

Create a new text file and type the following exactly as shown:

#!usr/bin/perl

print "Enter your name: ";
$name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl addict!
";

Save the file as hello.pl to a location of your choice. You don't have to use the .pl extension. In fact, you don't have to provide an extension at all, but it's good practice and helps you locate your Perl scripts easily later on.

Run Your Script

Back at the command prompt, change to the directory where you saved the Perl script. In DOS. you can use the cd command to move to the specified directory. For example:

cd c:\perl\scripts

Then type:

perl hello.pl

to run your script. If you typed everything exactly as shown, you are prompted to enter your name.

When you press the Enter key, Perl calls you by your name (in the example, it is Mark) and give you a dire warning.

C:\Perl\scripts>perl hello.pl

Enter your name: Mark

Hello, Mark
... you will soon be a Perl addict!

Congratulations! You have installed Perl and written your first script. You might not understand exactly what all those commands you typed mean yet, but you'll understand them soon.