Perl Programming/Getting started

Getting Started
This book assumes that you know absolutely nothing about programming at all and that Perl is your first language. However, basic operations such as making text files are outside of the realm of this tutorial.

Obtaining Perl
To find out, if you already have Perl installed on your computer, go into the command line and type:

This will display which version of Perl you have installed on your computer, if it is installed.

There are at least two easy ways to install Perl on Windows: the ActiveState distribution, and the Strawberry Perl distribution. Both are downloadable as native Windows installers. ActivePerl has a prebuilt package repository and is supported by a corporation, while Strawberry Perl includes a compiler (gcc) so that perl modules can be installed "on the fly" and is community-supported.

Most Unix-like operating systems will include Perl by default, and Linux Standard Base mandates that all compliant Linuxes ship with Perl installed. However, if for some reason you don't have perl, you can explore the options available to you at the main Perl download page, which will provide links to source and binaries.

A sample program
Perl is an interpreted language, which means you will always need the Perl interpreter which will compile and execute your program each time you run it. Instead of compiling your program into bytecode, like in C++ or Pascal, and then executing it, you can simply copy your program's source code to a different computer (that has the Perl interpreter) and run it.

For our first example, run your favorite text editor, and type something like this:

If you don't understand this yet, don't worry; This will be explained in more depth later.

Save the file as myprog.pl and you have a Perl program ready to run.

Windows
To run a Perl program with a modern version of ActivePerl installed, you simply click on it. If the screen flashes and you can't see the output you might have to execute the file from within the windows shell (ie. cmd.exe or PowerShell). With Strawberry Perl, you'll have to execute a Perl program from the command line as shown below.

From a Windows command-line interface, you can run the program thusly:

C:\> perl path\to\foo\myprog.pl

or, if perl.exe is not in your path:

C:\> c:\perl\bin\perl.exe myprog.pl

Note: You may have to specify the full path to your program unless you are running the command prompt in that directory.

UNIX-like systems
You can run a Perl program by running perl itself, and telling the shell the name of the file:

perl myprog.pl

Usually, Perl programs are made executable on their own. This involves two changes to the sample program. First, edit it and put the following shebang line at the top of the file:

Then, at a command prompt, make your program executable by using chmod.

Your program is now executable and ready to run, just like any other file. To execute, type:

By convention, .pl identifies a Perl script, and .pm a Perl library. The .pl file extension isn't needed for either of these examples; it's just a useful way of identifying files. The only time the convention should be violated is if the program is to be installed outside of the current working directory, and there runs a chance you might want to some day rewrite them in a different language.