1. Computing & Technology

How To Install Perl and Run Your First Script

From , former About.com Guide

4 of 4

Write And Run Your First Script

Use Your Favorite Text Editor

All you need to write Perl programs is a text editor. If you're on Windows, Notepad will do. Others include Vi, emacs, Textmate, Ultra Edit, Notepad++ ... there are a zillion different text editors out there all with their strengths and weaknesses, but if they can handle text they can handle Perl.

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 which can confuse programming languages like Perl.

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 it 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 your Perl script. In DOS you can use the cd command to move to the specified directory, eg:

cd c:\perl\scripts

Then type:

perl hello.pl

to run your script. If you have typed everything exactly as shown above, you'll be prompted to enter your name.

When you press the enter key, Perl will call you by name 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 that's what the rest of this site is for!)

©2012 About.com. All rights reserved.

A part of The New York Times Company.