How to Install Perl and Run Your First Script

Developers working in an office.
vgajic / Getty Images

Take your first steps into the fascinating world of Perl by setting up Perl on your computer and then writing 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.

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. 

Download and Install Perl

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

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

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.

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.

Write and Run Your First Script

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.

Format
mla apa chicago
Your Citation
Lewin, Mark. "How to Install Perl and Run Your First Script." ThoughtCo, Aug. 28, 2020, thoughtco.com/how-to-install-and-run-perl-2641103. Lewin, Mark. (2020, August 28). How to Install Perl and Run Your First Script. Retrieved from https://www.thoughtco.com/how-to-install-and-run-perl-2641103 Lewin, Mark. "How to Install Perl and Run Your First Script." ThoughtCo. https://www.thoughtco.com/how-to-install-and-run-perl-2641103 (accessed March 19, 2024).