Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
-
Upload
arabella-woods -
Category
Documents
-
view
220 -
download
0
Transcript of Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
![Page 1: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/1.jpg)
Introduction to Perl
Part III
By: Cedric Notredame
Adapted from (BT McInnes)
![Page 2: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/2.jpg)
Hashes
Hashes are like array, they store collections of scalars
... but unlike arrays, indexing is by name (just like ine real life!!!)
Two components to each hash entry: Key example : name Value example : phone number
Hashes denoted with % Example : %phoneDirectory
Elements are accessed using {} (like [] in arrays)
![Page 3: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/3.jpg)
Hashes continued ...
Adding a new key-value pair $phoneDirectory{“Shirly”} = 7267975
Note the $ to specify “scalar” context! Each key can have only one value
$phoneDirectory{“Shirly”} = 7265797 # overwrites previous assignment
Multiple keys can have the same value
Accessing the value of a key $phoneNumber =$phoneDirectory{“Shirly”};
![Page 4: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/4.jpg)
Hashes and Foreach Foreach works in hashes as well!
foreach $person (keys (%phoneDirectory) ) {
print “$person: $phoneDirectory{$person}”;}
Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!!
![Page 5: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/5.jpg)
Hashes and Sorting The sort function works with hashes as well Sorting on the keys
foreach $person (sort keys %phoneDirectory) {
print “$person : $directory{$person}\n”;
} This will print the phoneDirectory hash table in
alphabetical order based on the name of the person, i.e. the key.
![Page 6: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/6.jpg)
Hash and Sorting cont... Sorting by value
foreach $person (sort {$phoneDirectory{$a} <=> $phoneDirectory{$b}} keys %phoneDirectory)
{print “$person :
$phoneDirectory{$person}\n”;
}
Prints the person and their phone number in the order of their respective phone numbers, i.e. the value.
![Page 7: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/7.jpg)
Exercise
Chose your own test or use wget “http://www.trinity.edu/~mkearl/family.html”
Identify the 100 most frequent words
![Page 8: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/8.jpg)
Counting Wordswhile ($l=<F>)
{
@w=split (/\s+/, $l);
foreach $word (@w)
{
$word=~s/[sx]$//;#plurial elimination
$seen{$word}++;
}
}
foreach $word (sort {$seen{$a}<=>$seen{$b}} keys %seen)
{
print “Word $word N: $seen{$word}\n”;
}
![Page 9: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/9.jpg)
Command Line Arguments
Command line arguments in Perl are extremely easy. @ARGV is the array that holds all arguments passed
in from the command line. Example:
% ./prog.pl arg1 arg2 arg3 @ARGV would contain ('arg1', arg2', 'arg3)
$#ARGV returns the number of command line arguments that have been passed. Remember $#array is the size of the array!
![Page 10: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/10.jpg)
Quick Program with @ARGV
Simple program called log.pl that takes in a number and prints the log base 2 of that number;
#!/usr/local/bin/perl -w$log = log($ARGV[0]) / log(2);print “The log base 2 of $ARGV[0] is $log.\n”;
Run the program as follows: % log.pl 8
This will return the following: The log base 2 of 8 is 3.
![Page 11: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/11.jpg)
$_
Perl default scalar value that is used when a variable is not explicitly specified.
Can be used in For Loops File Handling Regular Expressions
![Page 12: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/12.jpg)
$_ and For Loops
Example using $_ in a for loop
@array = ( “Perl”, “C”, “Java” );for(@array) { print $_ . “is a language I know\n”;}
Output : Perl is a language I know. C is a language I know. Java is a language I know.
![Page 13: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/13.jpg)
$_ and File Handlers
Example in using $_ when reading in a file;
while( <> ) { chomp $_; # remove the newline char @array = split/ /, $_; # split the line on white space
# and
stores data in an array}
Note: The line read in from the file is automatically store in
the default scalar variable $_
![Page 14: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/14.jpg)
$_ and File Handling cont..
Another example similar to the previous example:
while(<>) { chomp; # removes trailing newline chars @array = split/ /; # splits the line on white
# space and stores the data
# in the array }
Notes: The functions chomp and split automatically perform their
respective operations on $_.
![Page 15: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/15.jpg)
Example Program
Count the number of words in a text and display the top 10 most frequency words.#!/usr/local/bin/perl
%vocab = (); $counter = 0;
while(<>) {
chomp;
foreach $element (split/ /) { $vocab{$element}++; }
}
foreach $word (sort {$vocab{$b}<=>$vocab{$a}} %vocab) {
print “$word $vocab{$word}\n”;
if($counter == 10) { last; } $counter++;
}
![Page 16: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/16.jpg)
$_ and Regular Expressions
Example in using $_ when using regular expressions
while( <> ) { chomp;
$_=~s/[.!,;]/ /; $_=~s/I am/Why are you/;
print “$_?\n”;}
Input line : I am feeling down today.
Output : Why are you feeling down today?
![Page 17: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/17.jpg)
Perl Modules
What are Perl Modules? Batches of reusable code Allow for object oriented Perl Programming
Comprehensive Perl Archive Network (CPAN) Perl utilities Perl Modules Perl documentation Perl distribution
![Page 18: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/18.jpg)
CPAN Organization
CPAN Material is organized by Modules Distributions Documentation Announcements Ports Scripts Authors
Distributions are ‘tar-gzipped’ tar gzip
![Page 19: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/19.jpg)
Categories of Modules
by-author Modules are organized by author’s registered CPAN
name by-category
Modules categorized by subject matter by-module
Modules categorized by module name
![Page 20: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/20.jpg)
Installing a Module
After you have gunziped and untared your module you have two options on installing your module depending upon if you have root privileges to the location where perl modules are installed or you don’t.
If you have root privileges or write access: perl Makefile.PL make make test make install
![Page 21: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/21.jpg)
Local Install
Need to specify where you would like the module to be installed by setting the PREFIX argument when generating a Makefile from Makfile.PL
perl Makefile.PL PREFIX=/home/Perl/Modules make make test make install
![Page 22: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/22.jpg)
Local Install cont…
Perl usually looks in system wide areas for modules, therefore it will not find your local module unless you tell Perl where to find it.
In your perl program where you will be using your module
#!/usr/local/bin/perl
use lib ‘<module location>’
use ModuleName;
![Page 23: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/23.jpg)
‘using’ a Perl Module
If we have a module called TestModule that contains a function test_me(). To use this module we have two options:
Object Orienteduse TestModule;$test = TestModule->new()$test->test_me()
Standarduse TestModuletest_me();
![Page 24: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/24.jpg)
Example Program
#!/usr/local/bin/perl
use lib ‘home/cs/bthomson/PerlModules/Suffix.pm’
use Suffix.pm
$sarray->Array::Suffix->new();$sarray->create_files(“hamlet.txt”);$sarray->get_ngrams();
![Page 25: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/25.jpg)
Module Documentation
Perl Module Documentation is provided by the module author and usually written in pod format
To view the documentation on the command line %perldoc modulename
Convert the pod document to the format of your choice: pod2text converts to a text file pod2html converts to an html file pod2man converts to a man page file
![Page 26: Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)](https://reader034.fdocuments.in/reader034/viewer/2022051415/56649de55503460f94add688/html5/thumbnails/26.jpg)
Thank you