Week 1: Perl Basics
Week 1: Perl Basics
Week 1: Perl Basics
What is Perl?
• The string-based nature of DNA and protein sequence data makes Perl an obvious
choice for many of the simpler problems in computational biology.
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:
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
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.
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.
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.
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
Hello world
Please type in the following program in using the editor as described above.
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
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.
use Getopt::Long;
Once you have finshed, save this file as helloMe.pl and then run it by typing:
Perl helloMe.pl –name yourname
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.