Your first Perl script#!/usr/bin/perl #This script prints a friendly greeting to the screen print “Hello World\n”; • Scripts are first “compiled” and then “executed” in the order in whic
Trang 1Introduction to Perl
Trang 2What is Perl?
• Practical Extraction and Report Language
• A scripting language which is both relatively simple to learn and yet remarkably powerful.
Trang 3Introduction to Perl
Perl is often described as a cross between shell
programming and the C programming language
C (numbers)
Shell programming (text)
Smalltalk (objects)
C++
(numbers, objects)
Perl (text, numbers) Java
Trang 4Introduction to Perl
• A “glue” language Ideal for connecting things
together, such as a GUI to a number cruncher, or a
database to a web server
• Has replaced shell programming as the most popular
programming language for text processing and Unix
system administration
• Runs under all operating systems (including Windows)
• Open source, many libraries available (e.g database,
internet)
• Extremely popular for CGI and GUI programming
Trang 5Why use Perl ?
• It is easy to gain a basic understanding of the language and start writing useful programs
Trang 6Why use Perl?
• Perl is free and available on all computing
platforms.
– Unix/Linux, Windows, Macintosh, Palm OS
• There are many freely available additions to Perl (‘Modules’).
• Most importantly, Perl is designed to
understand and manipulate text.
Trang 7Where to find help!
• http://www.perl.com
• http://www.perl.org
Trang 8Your first Perl script
#!/usr/bin/perl
#This script prints a friendly greeting to the screen
print “Hello World\n”;
• Scripts are first “compiled” and then “executed” in the order in which the lines of code appear
• You can write a script with any text editor The only rule is that it must be saved as plain text
Trang 9Running Perl Scripts
• Perl 5 is installed on our CS system
• Run from the command line:
palazzi% which perl /usr/bin/perl
palazzi$ perl hello.pl Hello world!
• You can run the script directly if you make the script executable, and the first line uses ‘hash-bang’ notation:
palazzi% chmod +x hello.pl palazzi% hello.pl
#!/usr/bin/perl -w print "Hello world!\n";
Trang 10Basic Syntax
• The -w option tells Perl to produce extra warning messages about potential dangers Always use
this option- there is never (ok, rarely) a good
reason not to.
#!/usr/bin/perl -w
• White space doesn't matter in Perl (like C++),
except for #!/usr/bin/perl -w which must start from column 1 on line 1.
Trang 11Basic Syntax
• All Perl statements end in a semicolon ; (like C)
• In Perl, comments begin with # (like shell
scripts)
– everything after the # to the end of the line is
ignored
– # need not be at the beginning of the line
– there are no C-like multi-line comments: /* */
Trang 12Perl Example
• Back to our “Hello World” program:
palazzi% hello.pl
#!/usr/bin/perl -w
# This is a simple Hello World! Program.
print "Hello world!\n";
– The print command sends the string to the screen, and “\n“ adds a new line
– You can optionally add parentheses:
print(Hello world!\n);
Trang 13First Script Line by Line
# This script prints a friendly greeting to the screen
• This is a Perl ‘comment’ Anything you type after a pound sign (#) is not interpreted by the compiler
These are notes to yourself or a future reader
Comments start at the ‘#’ and end at a carriage return
• #!/usr/bin/perl is NOT a comment (note this exception)
Trang 14First Script Line by Line
print “Hello World!\n”;
• This is a Perl ‘statement’, or line of code
• ‘print’ is a function - one of many
• “Hello World!\n” is a string of characters
– note the ‘\n’ is read as a single character
meaning ‘newline’
• The semicolon ‘;’ tells the interpreter that this line of code is complete.
Trang 15Many ways to do it!
# welcome.pl
print ( "1 Welcome to Perl!\n" );
print "2 Welcome to Perl!\n" ;
print "3 Welcome ", "to ", "Perl!\n";
print "4 Welcome ";
print "to Perl!\n";
print "5 Welcome to Perl!\n";
print "6 Welcome\n to\n\n Perl!\n";
Trang 16System Calls
• You can use Perl to execute shell
commands, just as if you were typing them
on the command line.
• Syntax:
– `command` # note that ` is the ‘backtick’
character, not the single quote ‘
Trang 17A script which uses a system call
• Note we are now using a ‘variable’ to hold the results of our system call
#!/usr/bin/perl
$directory_listing = `ls -l `;
print $directory_listing;
Trang 18Perl Variables and Truth
Trang 19What is a variable?
• A named container for a single value
– can be text or number
– sometimes called a ‘scalar’
• A scalar variable has the following rules
– Must start with a dollar sign ($)
– Must not start with a number
– Must not contain any spaces
– May contain ‘a’ through ‘Z’, any number character, or the ‘_’ character
Trang 21• $msg=“My name is $name”;
• In most cases, Perl determines the type (numeric
vs string) on its own, and will convert
automatically, depending on context (eg, printing
vs multiplying)
Trang 22Scalar variable names
• These are valid names
Trang 23Variable name tips
• Use descriptive names
– $sequence is much more informative than $x
– $sequence1 is ok $sequence_one is fine too
• Avoid using names that look like functions
– $print is probably bad (it will work!)
• Try to avoid single letter variable names
– $a and $b are used for something else
– Experienced programmers will often use $i and
$j as ‘counters’ for historical reasons
Trang 24Operator Description Example Result
. String concatenate 'Teddy' 'Bear' TeddyBear
= Assignment $bear = 'Teddy' $bear variable contains 'Teddy'
Trang 25A Perl calculator
#!/usr/bin/perl
$value_one = shift; #Takes the first argument from the command line
$value_two = shift; #Takes the next argument from the command line
$sum = $value_one + $value_two;
$difference = $value_one - $value_two;
$product = $value_one * $value_two;
$ratio = $value_one / $value_two;
$power = $value_one ** $value_two;
print "The sum is: $sum\n";
print "The difference is: $difference\n";
print "The product is: $product\n";
print "The ratio is: $ratio\n";
print "The first number raised to the power of the second number is: $power\n";
print ("I could have also written the sum as:", $value_one + $value_two, "\n”);
Trang 26• Special chars: $,@,%,&,”
• Use single quotes to avoid interpolation:
– print ‘My email is bhecker@acm.org Please send me $’;
– (Now you need to escape single quotes.)
• Another quoting mechanism: qq() and q()
– print qq(She said “Nortel cost \$$cost \@ $time”.);
– print q(My email is bhecker@acm.org Please send me $);
– Useful for strings full of quotes
Trang 27Backquotes: Command Substitution
• You can use command substitution in Perl like in shell
scripts:
$ whoami bhecker
Hi bhecker! There are 6 users logged on
• Command substitution will usually include a new line, so
Trang 28$nword = `wc -w $big | cut -c6-8 `;
chomp($nword);
$nchar = `wc -c $big | cut -c6-8 `;
chomp($nchar);
print "The biggest file in $dir is $big.\n";
print "$big has $nline lines, $nword words, $nchar characters.\n " ;
$ big1
The biggest file in /homes/horner/111/perl is big1.
Trang 29Quotes and more Quotes - Recap
• There is a fine distinction between double quoted
strings and single quoted strings:
– print “$variable\n” # prints the contents of $variable
and then a newline
– print ‘$variable\n’ # prints the string $variable\n to the
screen
• Single quotes treat all characters as literal (no
characters are special)
• You can always specify a character to be treated
literally in a double quoted string:
– print “I really want to print a \$ character\n”;
Trang 30Even more options
• the qq operator
– print qq[She said “Hi there, $stranger”.\n] ; #same as
– print “She said \”Hi there, $stranger\”.\n” ;
• qq means change the character used to denote the
string
– Almost any non-letter character can be used, best to pick
one not in your string
• print qq$I can print this string\n$;
• print qq^Or I can print this string\n^;
• print qq &Or this one\n&;
– perl thinks that if you use a ‘(‘, ‘[‘, or ‘{‘ to open the
string, you mean to use a ‘)’, ‘]’, or ‘}’ to close it
Trang 31What is Truth?
• A question debated by man since before cave art.
• A very defined thing in PERL.
– Something is FALSE if:
• a) it evaluates to zero
• b) it evaluates to ‘’ (empty string)
• c) it evaluates to an empty list (@array = “”)
• d) the value is undefined (ie uninitialized variable)
– Everything else is TRUE
Trang 32Numeric Comparison Operators
Operator Description Example Result
<= Greater Than or Equal 3<=2 TRUE
>= Less Than or Equal 3>=2 FALSE
<=> Comparison 3 <=> 2 1
" 2 <=> 3 -1
" 3 <=> 3 0
• Do not confuse ‘=‘ with ‘==‘ !!!!
•<=> is really only useful when using the ‘sort’ function
Trang 33String (Text) Comparison
Operators
•cmp is really only useful when using the ‘sort’ function
Operator Description Example Result
eq Equality 'cat' eq 'cat' TRUE
ne Non Equality 'cat' ne 'cat' FALSE
gt Greater Than 'data' gt 'cat' TRUE
lt Less Than 'data' lt 'cat' FALSE
ge Greater Than or Equal 'data' ge 'cat' TRUE
le Less Than or Equal 'data' le 'cat' FALSE
cmp Comparison 'data' cmp 'cat' 1
" 'cat' cmp 'data' -1
" 'cat' cmp 'cat' 0
Trang 34What did you mean?
• To make your life ‘easier’, Perl has only one
data type for both strings (characters) and numbers.
• When you use something in numeric context,
Perl treats it like a number
– $y = ‘2.0’ + ‘1’; # $y contains ‘3’
– $y = ‘cat’ + 1; # $y contains ‘1’
• When you use something in string context,
perl treats it like a string.
– $y = ‘2.0’ ‘1’; # $y contains ‘2.01’
• In short, be careful what you ask for!!
Trang 36• Functions are little bundles of Perl code
with names They exist to make it easy to
do routine operations
• Most functions do what you think they do,
to find out how they work type:
– perldoc -f function_name
Trang 37A Perl Idiom - if
• if is a function which does something if a
condition is true.
– print “Number is 2” if ($number == 2);
• Of course, there is also a function that does the opposite - unless
– print “Number isn’t 2” unless ($number == 2);
• You don’t ever need to use unless, unless
you want to
– print “Number isn’t 2” if ($number != 2);
Trang 38More about if
• A frequent Perl construction is the
if/elsif/else construct
– if (something){ do something }
– elsif (something else) { do something }
– else { do the default thing }
• The block of code associated with the first
true condition is executed.
• Note: elsif, not elseif
Trang 40print “Class at four.\n”;
} elsif ($today eq “Friday”) {
print “See you at the bar.\n”;
} else {
print “What’s on TV?\n”;
}
Trang 41Control flow
You’ve already seen a while loop.
for loops are just like C:
for ($i=0; $i<10; $i++) {
print “i is $I\n”;
}
Trang 42Getting at your data (Input and Output)
Trang 43A brief Diversion
• Get into the habit of using the -w flag
– mnemonic (Warn me when weird)
• Enables more strict error checking
– Will warn you when you try to compare strings numerically, for example
• Usage
– command line: ‘perl -w script.pl’
• even more diversion: ‘perl -c script.pl’ compiles but
does not run script.pl
– Or line: #!/usr/bin/perl -w
Trang 45Data flow
• Unless you say otherwise:
– Data comes in through STDIN (Standard IN)
– Data goes out through STDOUT (Standard Out) – Errors go to STDERR (Standard Error)
• Error code contained in a ‘magic’ variable $!
Trang 47• A shortcut to do both operations in one line is:
chomp($name = <STDIN>);
Trang 48$area = $height * $width;
print "The area of the rectangle is $area\n";
$ test.pl
Enter height of rectangle: 10
Enter width of rectangle: 5
The area of the rectangle is 50
$ test.pl
Enter height of rectangle: 10.1
Trang 49• The most common use of while is for
reading and acting on lines of data from a file
Trang 50#while_count.pl while ($val < 5){
Trang 51Shortcut operators
• Sometimes called auto operators (auto-increment, auto-decrement)
• Optimized for speed and efficiency
Operator Usage Read as:
++ $i++ $i = $i + 1 $i $i = $i - 1 += $i += 20 $i = $i + 20 -= $i -= 5 $i = $i - 5
*= $i *= 2 $i = $i * 2 /= $i /= 2 $i = $i / 2
.= $i = 'foo' $i = $i 'foo'
Trang 53• A filehandle is a way to interact with input or output
– ‘<>’ interacts with files on the command line
• filehandle names are simple strings with no symbols
– I usually use all caps (SEQFILE), but that isn’t
necessary
• You must open your filehandle before using it
Trang 54Opening Filehandles
• Open a file for reading
– open NAME, “<filename”;
• This is default behavior, so you don’t actually need the ‘<‘
• Open file for writing
– open NAME, “>filename”; #open new file
• Warning: If filename already exists, it is overwritten!!
– open NAME, “>>filename”; # append to old file
Trang 55• Flexible coding
– I want to specify the file to open on the
command line, rather than hard coding it
$in_name = shift;
$out_name = shift;
open FILE, “<$in_name” or die “Couldn’t open $in_name for reading: $!\n”; open OUT, “>$out_name” || die“Couldn’t open $out_name for reading: $!\n”; while ($line = <FILE>){
chomp $line;
print OUT “Something about $line\n }
• Usage: <$> myscript.pl inputfile outputfile
Trang 56When do I use a filehandle?
• You can get away with not using them, mostly
– STDIN is fine (<>) and you can always capture your
STDOUT to a file with a redirect (>) on the command line – <$> myscript.pl file_in > file_out
• If you are using two input files for different purposes
or want more than one output file, you need
filehandles
– <> will slurp all the input files on command line!
– > on the command line will put all output to one file
Trang 57Perl as Duct Tape (the force that
glues the universe together)
• The STDOUT of one script can serve as the STDIN of another script
– use the pipe (‘|’) symbol to chain scripts together
• Nothing goes to the screen in between scripts
– instead, what would normally go to the screen is redirected and made the STDIN of the next script
Trang 58Lists and More Lists
(Perl Arrays)
Trang 59– or: my $variable = ‘value’;
• my sets the ‘scope’ of the variable Variable exists
only within the current block of code
• use strict and my both help you to debug errors,
and help prevent mistakes
Trang 60What is an array?
• A named container for a list of values
– can be text or number, or mix
– An array is an ordered list
• Array names follow the same rules as scalar variables
– No spaces
– a-Z 0-9 and ‘_’ only
– Cannot start with a number
Trang 61Making an array
• @my_array = (1,15,’cat’, 23, ‘blue’);
– Note this is a comma separated list, enclosed in parentheses The parentheses are very important!!
• A tricky way:
– @my_array = qw (1 15 cat blue);
• mnemonic: qw - ‘Quote Words’
• Remember no commas if you use qw!
Trang 62A picture might help
• @my_array = (1,15,’cat’, 23, ‘blue’);
• @my_array
0 1 2 3 4
1 15
‘cat’
23
‘blue’
Element # Contents
Trang 63Getting at the Array Elements
• @my_array = (5, ‘boo’, ‘16’, ‘hoo’);
• $my_array[1] contains ‘boo’
– Pay attention! The way this is written is important
• An array element is a single (scalar) value
• Starts with the $ sign (just like a scalar) not the @ sign
• Square braces indicate the array position (index, or
element number)
• Perl counts from zero!! First element is $my_array[0]
Trang 64Manipulating Array Elements
• You can do anything to an array element that you can do to a scalar.
– $my_array[2] = ‘scary’;
• Of course you can do an assignment (=)
• list now is (5, ‘boo’, ‘scary’, ‘hoo’)
– $string = $my_array[2].$my_array[1]
• $string contains ‘scaryboo’
– $my_array[5] = ‘16’;
• list now (5, ‘boo’, ‘scary’, ‘hoo’, ‘’, ’16’)
• your list is as long as it needs to be!