Introduction to Perl
and Perl Syntax
Learning Perl, Schwartz
Next Reading Assignment
"How Perl Saved the Human Genome
Project" -- by Lincoln Stein
• Perl overview
• Perl -- first line of program convention
• Scalar data
• Strings -- single and double quotes
• Warning flags
• Scalar variable names
• Perl is an interpreted language
– perl programs require an interpreter
– once the interpreter is installed on a system, all (most) perl programs
should run on it
• Contrast to a compiled program (such as C or C++)
– compiled to a “binary”, or “opaque binary”
– a binary is so close to machine language, it is extremely difficult to
reverse engineer (at a high-level) the algorithms implemented
– only binaries need to be transferred
– not portable across architectures (Windows program will not run on a
– Generally -- compiled programs are faster than interpreted programs
– However, processor speeds and available memory sizes have
increased to the point that in many bioinformatics applications "Perl is
Perl -- first line of a program
#!/usr/bin/apps/perl …. etc. – typical locations, but may be anywhere
• first line of a perl application
– allows for automatic invocation
– tells the UNIX operating system where to find the Perl interpreter
– without this line we could also explicitly specify interpreter
• The Windows version supports this too (according to the docs -- but it’s a
different mechanism. I suspect windows simply interprets the .pl extension -
- but I was unable to find this in the docs ).
After the first line, the interpreter will ignore all lines that begin with: #
print “Hello, world!\n”;
Newline = \n = carriage return
print #This is A CoMment – completely ignored!!
; # Notice how formatting andCommentsCanMake
print “Hello, world!\n”;
interpreter “usr/local/bin/perl” not found
./test.pl: Command not found.
%perl test.pl -- this works
Note that most statements are followed by the pesky semicolon:
print “Hello, world!\n” ;
scalar – simplest, most basic kind of data in perl
(as opposed to a list, or array that is composed
of multiple scalars)
floating point: 1.25, 255.00, -3.124E-24
integers: 0 69 236123321 236_123_321
$i = 236_123_321;
• contains any combination of any characters
Single quoted string literals [" '] key (not the [~`]
'' # null string
'Don\'t isn\'t end\'d \'till here'
'last character is backslash\\'
'no newline here\n'
Allows for variable interpolation
"barney" #same as 'barney'
"new line here\n"
"last character is a dble quote\""
Note in MSWord/Poweroint you have to turn off
straight quotes with smart quotes")
or you get this: “test” and ‘test’
• Special characters
\r carriage return
• concatenation of strings (.) period
"Go" . 'hawks'. ' '."\n" # Gohawks - with newline
String repetition (x) :
$i = "Hawks" x 3; # $i = HawksHawksHawks
5 x 4; # equals 5555
# the integer 5 is converted to a string
4 x 5.6 # equals 44444 (5.6) truncated to 5
Auto Conversion between
Numbers and Strings
Perl is useful for rapid prototyping, so it converts
strings/numbers to numbers/strings depending
on operator context (this is not necessarily a
good thing -- so you have to be careful)
5 + 5 # equals 10
5 . 5 # equals '55'
"12" * "3" # equals 36
"12fred34" * '3' # equals 36
"fred" * "3" # equals 0 (these are all silly cases)
C counter example
char c = 'f';
int i = 3;
int j = 0;
j = c*i;
% perl –w program.pl
Argument "12fred34" isn't numeric in multiplication (*) at ./testl.pl line
Warnings are very, very good – but note that the program still
Checks for ‘unsafe’ constructs/mistyping.
How do you specify "-w" in Eclipse?
A variable contains/holds a scalar value
Cannot start with a number
All scalar variables are demarcated by $
Choose good Variable Names
$line_length #underscore is convention
$lineLength #works too
$fred = 17; # $fred variable has interger 17
$word = "Word";
$word = 3 * $fred; # word == 51
$words = $fred . "Word". $word; # "17Word51"
$word = "$fred is prime"; ## does this work???
$word = '$fred is prime'; #how about this? 17
Scalar variable name in string is replaced with
$isu = "a team that needs to improve";
$hawks = "will the hawks beat $isu";
# $hawks == "will the hawks beat a team that
needs to improve"
$hawks = 'will the hawks beat' . $isu;
print 'I like $money'; # prints dollar-sign
Comparison numeric string
equal == eq
not equal != ne
less than < lt
greater than > gt
less than/ <= le
greater than/ >= ge
Want to make decisions … Friday