up
Perl Lecture Notes - September 28
- Questions
- Housekeeping
- putting your work on bob: permissions and html directory
- submitting assignments
- Perl Documentation
- man perl; perldoc; pod2*
- online: see
wiki PerlStuff
- camel book as a reference
- don't underestimate "ask Google"
- This week's perl syntax
- Jim's advice: explore; play around; take what you need.
- The llama is a tour. For the complete picture, read the camel.
- llama chap 1 - 3 high lights :
- $_ , Perl's default ('it') variable,
as in " print; ", i.e. "print it"
- warnings : "perl -w" or "#!/usr/bin/perl -w"
- string interpolation : " His name is $name "
- size of array: "scalar @a" = number of element; $#array = index of last element. (But in perl people more often loop over arrays with things like 'foreach' or 'map' which don't mention the length explicitly.)
- array functions: push, pop, shift, unshift, map, grep,
order, reverse, join, sort, ...
- @a= (1, 100, 10, 1000 );
- $left = shift @a;
- $right = pop @a;
- push @a, $newRightmost;
- unshift @a,$newLeftmost;
- @b = grep { $_ > 10 } @a;
- @names = qw( George Mary Ringo Charlie );
- print join ' ', reverse sort map {lc} @names;
- llama chap 4 : Subroutines
- another funny character: & (which you can usually omit)
- all arguments passed in through special array @_
- modifying elements of @_ (i.e. $_[0], $_[2], ...)
changes values in calling routine
- create private copies with "my ($a,$b) = @_" syntax.
(highly recommended.)
- "local" keyword : ignore this for now;
you almost never need it.
- "my", "our", "local", "use vars qw($a $b $c)"
issues of namespaces, package global variables, 'lexical' variables
-
# Typical style for subroutine definitions
sub addTwoNumbers {
my ($a,$b) = @_;
return $a + $b;
}
- llama chap 5 : Hashes
- another funny character: %
- gets used often in perl, like a private little database.
- auto-creation of elements
- can be used for counting things
- the big arrow: =>
- hash functions: keys, values, exists, delete
- %car = ( color => 'blue', length => 6 );
- foreach my $key (keys %car) {
print $key ." ". $car{$key} . "\n" };
- print " length is " . $car{length} if exists $car{length};
- llama chap 6 : Simple I/O and unix pipes
- unix pipes: this < input | filter | that gt; output
- STDIN, <>, STDOUT, print : file handles
- ( $a = <> ) vs ( @a = <> )
- <> and chomp()
- Weird command line switches: -p, -n , -i , -e . Example:
perl -pi.orig -e 's/this/that/' sample.txt
- printf, sprintf
- print FILEHANDLE "hello";
- CPAN
- See list of modules (also called packages or .pm files) at
uwinnipeg
- "perl -MCPAN -e shell"
a built-in automatic download, compile, install
- "are you ready for to configure" - just say "NO"
- getting the first piece - "force install Net::FTP"
- alternative : download a module like Net-FTP.tar.gz;
upack with something like "gnutar zxf file.tar.gz";
then "perl Makefile.PL; make; make test; make install"
- For more info, see chapter 22 on CPAN in the camel book
- Examples
- Assignment:
- Read chapter 4, 5, 6 in the llama book,
on subroutines, hashes, and simple I/O
- Browse chapters 1-4 in the camel book.
This book also covers the basics of the language,
but in a more thorough way, with more detail.
Get a feel for what's in there, particularly for use as a
reference. The camel is the authority for Perl.
- Pick a CPAN module that looks interesting to you.
(See, for example, the list
here.)
Install it on your linux box or at home
("perl -MCPAN -e shell" is usually the easiest method), and
write a short program to try it out.
- In the llama book, do exercises 4.1, 4.2, 5.2, 6.1