Transcript Slide 1

Introduction to Perl
and Perl Syntax
Learning Perl, Schwartz
1
Next Reading Assignment
"How Perl Saved the Human Genome
Project" -- by Lincoln Stein
http://www.foo.be/docs/tpj/issues/vol1_2/tpj0
102-0001.html
2
XKCD
3
Outline
•
•
•
•
•
•
•
•
Perl overview
Perl -- first line of program convention
Comments
Scalar data
Strings -- single and double quotes
Warning flags
Scalar variable names
Interpolation
4
Perl
• 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
Mac, etc)
• Performance
– Generally -- compiled programs are faster than interpreted programs
(Perl, Java)
– However, processor speeds and available memory sizes have
increased to the point that in many bioinformatics applications "Perl is
good enough."
– Perl is notorious for being “quick and dirty”
– However, well written perl can be elegant and “beautiful” too
5
Perl
• Perl is also a popular tool for system
administration
• Simple file I/O syntax
• "Pipes" and file re-direction
• Perl is often the "glue" between complex software
systems
• Example – SAN server load tester
6
Perl -- first line of a program
#!/usr/bin/perl
(optional)
OR
#!/usr/local/bin/perl
OR
#!/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
– Ex)
my_program.pl
– without this line we could also explicitly specify interpreter
/usr/bin/perl ./my_program.pl
OR
perl my_program.pl
•
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 ).
7
Perl
After the first line, the interpreter will ignore all lines that begin with: #
#!/usr/local/bin/perl
print “Hello, world!\n”;
Newline = \n = carriage return
Hello, world!server06%
#!/usr/local/bin/perl
print #This is A CoMment – completely ignored!!
“Hello, world!\n”
; # Notice how formatting andCommentsCanMake
#itMoreDifficult/EasierToRead
8
Perl
#!usr/local/bin/perl
print “Hello, world!\n”;
% ./test.pl
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”
;
9
Scalar Data
scalar – simplest, most basic kind of data in perl
(as opposed to a list, or array that is composed
of multiple scalars)
Numbers
floating point: 1.25, 255.00, -3.124E-24
integers: 0 69 236123321 236_123_321
$i = 236_123_321;
print “$i\n”;
236123321
10
Strings
• contains any combination of any characters
Single quoted string literals [" '] key (not the [~`]
key):
'fred'
'barney'
''
# null string
'Don\'t isn\'t end\'d \'till here'
'last character is backslash\\'
'no newline here\n'
11
Strings
Double-quoted literals
Allows for variable interpolation – the substitution of
variables within a string
$i = "barney";
$j = "fred and $i";
"new line here\n"
"last character is a dble quote\""
Note in MSWord/Poweroint you have to turn off
Tools->AutocorrectOptions>AutoFormatAsYouType->(uncheck "replace
straight quotes with smart quotes")
or you get this: “test” and ‘test’
12
Strings
• Special characters
\n newline
\t tab
\r carriage return
many others
13
Strings
• concatenation of strings (.) period
"Go" . 'hawks'. ' '."\n" # Gohawks - with newline
String repetition (x) :
$i = "Hawks" x 3; # $i = HawksHawksHawks
5 x 4;
4 x 5.6
# equals 5555
# the integer 5 is converted to a string
# equals 44444 (5.6) truncated to 5
14
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)
15
C example – char is still an int
main()
{
char c = 'f';
int i = 3;
int j = 0;
j = c*i;
printf("%d\n",j);
}
16
Warnings
% perl –w program.pl
OR
#!/usr/local/bin/perl –w
Argument "12fred34" isn't numeric in multiplication (*) at ./testl.pl line
18.
Warnings are very, very good – but note that the program still executes.
Checks for ‘unsafe’ constructs/mistyping.
How do you specify "-w" in Eclipse?
17
Scalar Variables
A variable contains/holds a scalar value
(number/string).
Cannot start with a number
Case sensitive
All scalar variables are demarcated by $
$i
$This_is_aVariable259-1
$A
$a
18
Choose good Variable Names
$i
$line_length #underscore is convention
$lineLength #works too
Assignment
$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?
19
Interpolation
Scalar variable name in string is replaced with
current value.
$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
20
Comparison
Comparison
numeric string
equal
not equal
less than
greater than >
less than/
equal to
greater than/
equal to
==
!=
<
eq
ne
lt
gt
<=
le
>=
ge
21
Control Structure
Want to make decisions … next lectures
22
#!/usr/bin/perl
$sleep_count=24;
$in = "";
$out = "";
$|=1; # flush the output buffer
#$path = "/Volumes/ReadWriteTest";
$path = "/Volumes/Macintosh
HD/Users/tabraun/perl/ReadWriteTest";
print "Testing SAN read/write on $path\n";
$host = `hostname`;
chop($host);
while($sleep_count>0) {
#$arraySize=100_000_000; #made shorter -- just for
demo
$arraySize=10_000; #made shorter -- just for demo
$count=10_000;
print "allocating big array\n";
while($arraySize > 0) # just allocate a big array to
{
# stress test memmory
$array[$arraySize]=$arraySize;
$arraySize=$arraySize-1;
print "arraySize = $arraySize\r";
}
while($count>0) {
$file = 'write-'.$host.'-'.$count;
open(WRITE,">$path"."/"."$file.txt");
$out="";
for($j=0;$j<100;$j++) # 100 lines
{
for($k=0;$k<6;$k++) #60 characters per line (6 * 10)
{
for($i=0;$i<10;$i++) # digits 0-9
{
$out=$out.$i;
print WRITE $i;
}
}
$out=$out."\n";
print WRITE "\n";
}
close WRITE;
23
open(READ,"$path/$file.txt") || die ("Cannot open
$path/$file.txt\n");
$in="";
while($line = <READ>)
{
$in = $in.$line;
}
if($in == $out) {
print "No failures iteration $count\n";
}
else{
print "Error -- writing differences to errorWrite-$file.txt
and errorRead-$file.txt\n";
open E1,">$path/errorWrite-$file.txt";
open E2,">$path/errorRead-$file.txt";
print E1 "$out";
print E2 "$in";
close E1;
close E2;
}
close(READ);
$count=$count-1;
} # end while loop
printf("\n");
$x=10;
while($x>0)
{
print "sleeping for $x seconds\r";
$x--;
sleep(1); #sleep for an hour to keep the process alive
# for at LEAST 24 hours.
}
printf("\n");
$sleep_count=$sleep_count-1;
}
24