Perl Programming/Getting Started

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
  Index Next: First Programs

Contents

[edit] 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.

[edit] Obtaining Perl

The easiest way to install Perl on Windows may be to use the ActiveState distribution, which is downloadable as a native windows installer.

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.

[edit] Writing programs

[edit] A Sample Program

Perl is an interpreted language, this 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 Pascal or C++ 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:

#!/usr/bin/perl
use strict;
use warnings;
 
print "Hello World";

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.

[edit] Running programs

[edit] 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 with in the windows shell (ie. cmd.exe or PowerShell).

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.

[edit] 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:

#!/usr/bin/perl

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

chmod +x myprog.pl

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

./myprog.pl

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.

  Index Next: First Programs
Personal tools