Perl Programming for Biologists Spring, 2006
Week 1: Perl Basics
What is Perl?
• Perl stands for Practical Extraction and Report Language.
• Perl is an interpreted programming language optimized for scanning arbitrary text
files, extracting information from those text files, and printing reports based on that
information.
• The string-based nature of DNA and protein sequence data makes Perl an obvious
choice for many of the simpler problems in computational biology.
How do I install Perl on my Computer?
Perl is an application, just like any other windows applications. It can be downloaded for
free for your use at http://www.perl.com/ This is the main website for perl, and contains
many useful links.
Downloading Perl For Windows
If you are using a windows-based computer, you should go to the active state web site
(http://www.activestate.com/) and download the latest version of ActivePerl, which is
freely available. As of this writing, the latest version is ActivePerl 5.8.7 build 815.
Make sure to download the Windows MSI package. There are installation instructions
on the ActiveState website as to how to install ActivePerl. You should just be able to
download the appropriate package and run it, and the installation should automatically
occur.
Perl for Other Operating Systems
If you need the Mac version of Perl, you can go to http://www.macperl.com/ in order to
download Perl. Note that ActivePerl will now work on Macs as well!
If you are using a Linux or Unix based operating system, then chances are great that Perl
is already installed. To see if Perl is installed in Linux or Unix, type:
perl –v
If it is installed, you will get back a message giving you information concerning the
version you are using; otherwise, it will report back:
command not found
Eric Rouchka 1 University of Louisville
Perl Programming for Biologists Spring, 2006
ActiveState Download Page.
Adding Perl to your path
Installation of active state should automatically add Perl to your path. You can check this
by starting a command window (go to start->Run and type in “command”) and typing in
the word perl. If you get a message saying “command not found” then Perl is not in your
path. Otherwise, Perl might start and be awaiting input from you (you can type ctrl-c to
exit).
If Perl is not in your path, you can add it by editing the file C:\autoexec.bat. Adding perl
to your path will allow you to run perl by typing in only the command perl and not
D:\perl\bin\perl. To do this, you need to do two things. First, make a backup
copy of the file c:\autoexec.bat. This can be done in the dos window by typing
cp c:\autoexec.bat c:\autoexec.bak
Eric Rouchka 2 University of Louisville
Perl Programming for Biologists Spring, 2006
Now we need to edit the file c:\autoexec.bat file. This can either be done in
notepad which can be found under Start-> Program->Accessories or by typing in the
dos window:
edit c:\autoexec.bat
Now at the end of this file, add in the following line, substituting the correct location
of where Perl is installed. If you installed ActivePerl using the default locations, then
Perl is located in c:\Perl\Bin:
PATH=%PATH%;PATH TO PERL GOES HERE;
PATH=%PATH%;c:\perl\bin;
And save autoexec.bat. You may now need to reboot your computer for these
changes to take effect. Note that you only need to do this step once.
Creating Perl Programs
Perl programs can be created using any basic text editor, such as notepad. Perl programs
typically end with a “.pl” extension. Make sure that if you are saving perl programs in
notepad that you check it to save as all file types. Otherwise your perl programs will
have a .txt extension automatically added to them!
What is a Perl Program?
A program consists of a text file containing a series of Perl statements.
Perl statements are separated by a semi-colon.
Leading spaces on a line are ignored.
Multiple spaces, tabs, and blank lines are ignored.
Anything following # is ignored.
Perl is case sensitive.
Your First Perl Program
We will be starting with a simple program to get you started with learning what a Perl
program looks like, and how it is created and run.
How do I edit or create a Perl Program?
For now, you can use either notepad, edit, or any other simple text editor to create your
perl program. The editor you choose to use must save your files as plain text. Perl files
should always be saved with a .pl extension at the end.
Eric Rouchka 3 University of Louisville
Perl Programming for Biologists Spring, 2006
How do I run a Perl Program?
Perl programs are run by typing perl followed by the program name and any command
line options. You should be running the perl programs from the command window,
which is accessed by Start->run and typing “command”. You should then change the
directory (using the command “cd” ) to the directory where the perl program is located.
For instance, if you save the program “hello.pl” in the directory
c:\PerlPrograms\ you would type: cd c:\PerlPrograms
You can then run “hello.pl” by typing perl hello.pl
Hello world
Please type in the following program in using the editor as described above.
# This is my first Perl program!
print (“Hello World!\n”);
print (“This is my first Perl program.\n\n”);
Now that you have it typed in, we need to save and run the program. Save the program
as hello.pl and then run it by typing perl hello.pl
Understanding Hello World
print() is a function that displays text to the screen.
\n prints a new line to the screen; i.e. it goes to the start of the next line.
# signals the beginning of the comment – everything after it will be ignored
; separates the Perl statements from one another. Statements do not have to occur on
separate lines. This is done to make it easier for humans to read.
Updating Hello World
When you run a program, it is often desired to use different data. We will now update the
hello world program to use command line arguments that can be used to set different
options for a program. In this specific example, we will allow the user to enter in their
name, which then gets printed to the screen.
Edit the hello.pl file so it now looks like the following:
Eric Rouchka 4 University of Louisville
Perl Programming for Biologists Spring, 2006
use Getopt::Long;
#usage: perl helloMe.pl –name [NAME]
&GetOptions(“name=s” => \$Name);
# The above line lets the user enter in their name
print (“Hello $Name \n”);
print (“I now have written a perl program using command \n”);
print (“line arguments and variables \n\n”);
Once you have finshed, save this file as helloMe.pl and then run it by typing:
Perl helloMe.pl –name yourname
Understanding the Updated Version
use Getopt::Long; For now, we will just say that this line is used whenever we
want to retrieve command line arguments. Basically this line tells the perl interpreter that
we are using a function that is already defined in another location.
&GetOptions() is a function that retrieves the command line options. In this
example, it retrieves the value directly after the –name when the program is run. It
stores the value entered in in a variable called $Name. This value can be retrieved any
time in the program. We will talk about variables more in the next perl session.
Eric Rouchka 5 University of Louisville