Getting Started with Perl

If you don't already know, I guess I should tell you how to run Perl scripts. I can't tell you how hard that can be if you don't already know. It's true of many languages. You come across example code on the web, but how the heck do you run it? Do you need to install some software? How do you save that code to a file? How do you compile it? How do you launch it? Sorry, I guess I over looked this all this time. Let's fix that.

All of the Perl code on this website was developed and tested using ActiveState's Active Perl distribution. It's called a distribution, because Perl is open source, so there are any number of Perl distributions. ActiveState has gathered the source code, packaged it with extra modules developed by various people, done some testing, and compiled it all for various operating systems. The distribution for Windows is what we're using here. Sorry, but I don't think my Active Directory scripts will work on a Linux machine. The underlying Windows functionality is just not available on Linux.

Anyway, your first step is to download Active Perl. The download is available at http://www.activestate.com/activeperl/downloads

You should choose the 32 bit or 64 bit MSI installer, depending on what version of Windows you are running. Installing it is easy, after downloading the MSI, just double-click on it, and follow the prompts. You can practically just lean your elbow on the enter key during the install.

After Perl is installed, you can start writing scripts. Simply create a folder for your scripts (I keep mine in c:\scripts). The next thing you'll want to do is to tell Windows that you want to see the file extensions for known file types. This way, Windows won't put a hidden .txt on the end of your file names, which will keep your scripts from running. To do this, open your scripts folder, and from the menu, select Tools - Folder Options. Select the View tab and make sure that "Hide Extensions for Known File Types" is deselected as shown in the figure below.

Click OK. Now let's create a script. Right-click in your scripts folder, and click New - Text Document, and name it hello.pl (you'll be prompted to confirm that you really want to change the file extension, click Yes).

The file you just created will have the ActiveState Perl icon (it's a gecko, although you might think it looks like a space dolphin). Now right-click the file and select Edit. Notepad will open.

Notepad is my editor of choice; I'm just comfortable with how it looks and how it works. There are a number of more fully featured text editors out there, some that are Perl syntax aware, so they can display keywords in color and possibly highlight syntax errors as you type. I find all that annoying.

Anyway, you now have notepad open. Let's type that famous first line of code. Type this into your file:

print "Hello World!\n";

Save the file. Now how to run it? You can double-click the file, and it will run, but what you'll see is a command window flash open and close before you can see what happened. We want to see the results of our work. So open up a command prompt (if you don't know how to do that, just click Start - Run, type cmd and click OK. Now change directory to your scripts folder, and to run the script, type hello.pl

Unless you fat-fingered our one line of code, you should see Hello World! on the screen. That's how you do it. Now, when you see a piece of Perl code on this web site, you can simply select the code with your mouse, copy and paste it into a new .pl file, save the file and run it. Many of the scripts on this website will run in your environment without editing them at all. Some require you to change a thing or two, like the user name, the domain name, or something like that. I hope this information will help you get started with Perl, and will help you get more out of the examples on the site.

Happy coding!

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...