Introduction to Perl Part III - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Perl Part III

Description:

ARGV is the array that holds all arguments passed in from the command line. Example: ... ARGV would contain ('arg1', arg2', 'arg3) $#ARGV returns the number of ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 28
Provided by: csU86
Learn more at: https://www.d.umn.edu
Category:
Tags: iii | argv | introduction | part | perl

less

Transcript and Presenter's Notes

Title: Introduction to Perl Part III


1
Introduction to PerlPart III
  • By Bridget Thomson McInnes
  • 6 Feburary 2004

2
Hashes
  • Hashes are like array, they store collections of
    scalars
  • ... but unlike arrays, indexing is by name
  • 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)

3
Hashes continued ...
  • Adding a new key-value pair
  • phoneDirectoryShirly 7267975
  • Note the to specify scalar context!
  • Each key can have only one value
  • phoneDirectoryShirly 7265797
  • overwrites previous assignment
  • Multiple keys can have the same value
  • Accessing the value of a key
  • phoneNumber phoneDirectoryShirly

4
Hashes and Foreach
  • Foreach works in hashes as well!
  • foreach person (keys phoneDirectory)
  • print person phoneDirectoryperson
  • Never depend on the order you put key/values in
    the hash! Perl has its own magic to make hashes
    amazingly fast!!

5
Hashes and Sorting
  • The sort function works with hashes as well
  • Sorting on the keys
  • foreach person (sort keys phoneDirectory)
  • print person directoryperson\n
  • This will print the phoneDirectory hash table in
    alphabetical order based on the name of the
    person, i.e. the key.

6
Hash and Sorting cont...
  • Sorting by value
  • foreach person (sort phoneDirectorya ltgt
    phoneDirectoryb keys phoneDirectory)
  • print person phoneDirectoryper
    son\n
  • Prints the person and their phone number in the
    order of their respective phone numbers, i.e.
    the value.

7
A Quick Program using Hashes
  • Count the number of Republicans in an array
  • seen () initialize hash to empty
  • _at_politArray ( R, R, D, I, D, R, G
    )
  • foreach politician (_at_politArray)
  • seenpolitician
  • print Number of Republicans seen'R'\n

8
Slightly more advanced program
  • Count the number of parties represented, and by
    how much!
  • seen () initialize hash to empty
  • _at_politArray ( R, R, D, I, D, R, G
    )
  • foreach politician (_at_politArray)
  • seenpolitician
  • foreach party (keys seen)
  • print Party party. Num reps
    seenparty\n

9
Command Line Arguments
  • Command line arguments in Perl are extremely
    easy.
  • _at_ARGV is the array that holds all arguments
    passed in from the command line.
  • Example
  • ./prog.pl arg1 arg2 arg3
  • _at_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!

10
Quick Program with _at_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(ARGV0) / log(2)
  • print The log base 2 of ARGV0 is log.\n
  • Run the program as follows
  • log.pl 8
  • This will return the following
  • The log base 2 of 8 is 3.

11
Another Example Program
  • You want to print the binary form of an integer
  • !/usr/local/bin/perl -w
  • foreach integer (_at_ARGV)
  • converts the integer to a 32 bit binary
    number
  • _at_binarysplit//,unpack(B32,pack(N,integer))
  • Store the last 4 elements of _at_binary into
    _at_bits
  • _at_bits _at_binary28..binary
  • Print the integer and its binary form
  • print integer _at_bits\n

12
_
  • Perl default scalar value that is used when a
    variable is not explicitly specified.
  • Can be used in
  • For Loops
  • File Handling
  • Regular Expressions

13
_ and For Loops
  • Example using _ in a for loop
  • _at_array ( Perl, C, Java )
  • for(_at_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.

14
_ and File Handlers
  • Example in using _ when reading in a file
  • while( ltgt )
  • chomp _ remove the
    newline char
  • _at_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 _

15
_ and File Handling cont..
  • Another example similar to the previous example
  • while(ltgt)
  • chomp removes
    trailing newline chars
  • _at_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 _.

16
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(ltgt)
  • chomp
  • foreach element (split/ /) vocabelement
  • foreach word (sort vocabbltgtvocaba
    vocab)
  • print word vocabword\n
  • if(counter 10) last counter

17
_ and Regular Expressions
  • Example in using _ when using regular
    expressions
  • while( ltgt )
  • chomp
  • _s/.!,/ /
  • _s/I am/Why are you/
  • print _?\n
  • Input line
  • I am feeling down today.
  • Output
  • Why are you feeling down today?

18
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

19
CPAN Organization
  • CPAN Material is organized by
  • Modules
  • Distributions
  • Documentation
  • Announcements
  • Ports
  • Scripts
  • Authors
  • Distributions are tar-gzipped
  • tar
  • gzip

20
Categories of Modules
  • by-author
  • Modules are organized by authors registered CPAN
    name
  • by-category
  • Modules categorized by subject matter
  • by-module
  • Modules categorized by module name

21
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
    dont.
  • If you have root privileges or write access
  • perl Makefile.PL
  • make
  • make test
  • make install

22
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

23
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 ltmodule locationgt
  • use ModuleName

24
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 Oriented
  • use TestModule
  • test TestModule-gtnew()
  • test-gttest_me()
  • Standard
  • use TestModule
  • test_me()

25
Example Program
  • !/usr/local/bin/perl
  • use lib home/cs/bthomson/PerlModules/Suffix.pm
  • use Suffix.pm
  • sarray-gtArraySuffix-gtnew()
  • sarray-gtcreate_files(hamlet.txt)
  • sarray-gtget_ngrams()

26
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

27
Thank you ?
Write a Comment
User Comments (0)
About PowerShow.com