Perl basics for beginners (on Windows)
August 7th, 2010 3 Comments
I've been writing some Perl again recently. The scripts I've been writing have all been sitting on a Solaris server to which I've been connecting remotely. But today my connection was super slow (thanks Sky Broadband) so I decided to work locally on my PC for a change.
This meant getting Perl working on my laptop. It's pretty easy. So I thought I'd blog the steps involved.
Read on if you want to try writing some Perl but:
- You don't know how to get Perl set up
- You don't have access to a server where Perl is already set up
- You use Windows and you really don't want to become a Linux guru (at least not today)
There are a variety of ways to do this. This is just the way I used a few minutes ago. It uses XAMPP which means that, by the end of the process not only will you have Perl (one of the Ps in "XAMPP"), you'll also have the Apache Web Server, MySQL and PHP. In case you're wondering, the X apparently stands for cross-platform. You might not want all of these right now but, unless you're really tight for disk space, I'd advise getting them anyway. If you're ever going to do any Web development, having this package of technologies on your laptop, ready to switch on with the click of a button, is fantastically useful.
So here we go:
- Browse to http://www.apachefriends.org/en/xampp-windows.html
- Scroll down the page to the download section for XAMPP for Windows.
- Click the link for the EXE file (content description: "Self-extracting RAR archive").
- Once downloaded, double-click the xampp-win32-n.n.n.exe file and confirm you want to run it.
-
In the XAMPP for Windows dialog box, change the destination folder if required.
The installer installs XAMPP in an xampp directory within the destination you specify. So if you leave the destination at C:\ then you'll get a C:\xampp directory. I prefer to install things in D:\programs, so I changed the destination to this.

-
Click Install.
From here on it's just a case of following the on-screen instructions.
- After XAMPP is installed, open the Windows Control Panel and double-click System.
- Under Tasks, click Advanced system settings.
-
In the System Properties dialog box, click Environment Variables.

We're going to add the path to Perl to your system's Path environment variable, so that it knows where to find the Perl executable for running Perl programs.
-
In the Environment Variables dialog box, scroll down the list of system variables and double-click the entry for Path.

-
In the Edit System Variable dialog box, click in the Variable value field, move the cursor to the end of the text and add a semicolon followed by <destination>\xampp\perl\bin. For example, if you installed xampp in C:\ you would enter
;C:\xampp\perl\bin
In my case, my path to Perl is D:\programs\xampp\perl\bin.

-
Click OK in this and the other two dialog boxes.
We'll now test that Perl is installed correctly.
-
Click the Windows button (that is, go to what used to be called the Start menu).
-
Enter cmd into the search box.
A command console window opens.
-
Enter: perl -v
If everything's working correctly you'll get a message that shows the Perl version that's installed - for example:
This is perl, v5.10.1

What this means is that, once you have a Perl script, you'll be able to cd to the directory in the command console and run the script by entering perl myscript.pl.
-
Close the command console window.
Now we're going to start doing the interesting stuff: writing a Perl script.
-
Open you're favourite text editor - for example, Notepad++.
-
Enter the following:
#!D:\programs\xampp\perl\bin\perl.exe
use strict; use warnings;
print "Hello world!\n"; print "Press Return to exit.\n";
my $userInput = <>;
Note: You'll need to change the first line to match the path to perl.exe on your computer. -
Save the file as myscript.pl.
-
Open Windows Explorer and navigate to the folder where you saved the script.
-
Right-click myscript.pl and choose Open With > Choose Default Program.
-
In the Open With dialog box, click Browse.
-
Browse to your perl\bin directory and double-click perl.exe.
-
In the Open With dialog box, select the check box labelled Always use the selected program to open this kind of file and click OK.
You've now associated the .pl file name extension with the perl.exe executable. Windows now knows to use perl.exe to run .pl files.
So let's test it out.
-
In Windows Explorer, double-click myscript.pl.
-
A command console window opens to run the script. Our two lines are printed to standard output (in layman's terms they're displayed) and (thanks to $userInput = <>) the script waits for you to enter something before exiting. If you didn't have this line the console window would just close and you wouldn't have time to read what was on the screen.
Job done. From now on you can write Perl scripts in your text editor and run them by just double-clicking the file in Windows Explorer.
So what?
Yeh, okay, so it's just another "Hello world!" program.
But now that you've got Perl set up and you know how to run a program, you can go off and learn to do lots of stuff with Perl.
Me, I'm going off to write a Perl script that will run every half an hour to: parse a table on a "Revision history" Web page within a WebHelp site generated from Madcap Flare, look for new rows in the table (i.e. details of new changes to the WebHelp) and, if it finds any, email the details to a mailing list and then alter the HTML of the Web page adding a style tag to the rows to indicate that the mailing list has been mailed the details.
I hope you find this useful. Drop me a comment to let me know.
Potentially similar posts
- Gotchas with running a Perl script as a cron job – August 2010
- Restoring Vista’s missing “Text Document” shortcut – April 2009
- Test a help topic without recompiling – December 2008
- Preventing BBC iPlayer running all the time – November 2008
- Revealing Vista’s hidden Administrator account – November 2008
August 8th, 2010 at 11:03 am (#)
Great post, Alistair. Another one I have used is http://www.activestate.com/activeperl, which works well on Windows.
August 9th, 2010 at 7:36 pm (#)
Raj, you're wrong. All of the above applies to Vista. My laptop runs Vista. All of the above relates to what I did myself on Vista - the screenshots kind of prove this, don't they? In Vista, go to the Windows Control Panel and double-click System. On the left hand side there's a list headed Tasks, with an option "Advanced system settings".
But thanks for commenting anyway.
August 9th, 2010 at 7:37 pm (#)
Yeh, good call. I used ActivePerl for years and it's another great option.
Thanks for that Hamish.