Title: Introduction to PERL modules
1Introduction to PERL modules
There is more than one way to do it
Larry Wall
2Contents
- Subroutines
- Procedures vs functions
- Internal or external?
- Packages/Modules
- Namespace/Typeglob
- Import/Export
- Includes
- Objects?
- In-liners
- Examples
- Exercises
3Subroutines (procedure, function)
- defining subroutine
- sub myfunc
- my param shift(_at__)
-
- return result
-
- calling function
- calcul myfunc(value)
- defining subroutine
- sub myproc
- my param shift(_at__)
-
- return
-
- calling procedure
- myproc(value)
4Call internal code?
f(param) call
f(param) call
sub f
sub f
5Call external code!
use Mymod Mymodf(param) call
sub f
use Mymod Mymodf(param) call
6Create a module
- Structure of module Mymod.pm
- package Mymod
- sub f
- sub g
-
- 1
- Calling the functions in a script
- use Mymod
-
- Mymodf(param)
- a Mymodg()
7Namespace
- Symbol table
- Every variable, function, handler is stored in a
table of symbols (namespace) - use can pollute your namespace
- default package main
- Typeglob (or aliases)
- Possibility to import variables from one package
to another - OnePackdick \OtherPackrichard
- Thus dick function eq richard function
- dick \richard
- Thus dick eq richard, but _at_dick ne _at_richard !!
8Export symbols
two essential lines in a package require
Exporter our _at_ISA (Exporter) inherits
from Exporter export by default our _at_EXPORT
qw(cat canis carnivore) export on demand our
_at_EXPORT_OK qw(tiger)
9Import symbols
use Animals import all _at_EXPORT symbols use
Animals() import nothing use Animals qw(cat
tiger) import tiger and cat use Animals
qw(/ca/) import carnivore and cat use Animals
qw(/\/) import all scalars More generally,
use Animals LIST is equivalent to BEGIN
require Animals import Animals LIST
10Includes
- List of directories to search for modules
- _at_INC
- Modify with pragma lib before compilation
- use lib ENVHOME/libperl adds /libperl
- Once started the list of loaded modules is
available in a hash - INC
11Modules and Objects
- An object is a reference
- A class is a package
- A method is a function
- A class inherits through _at_ISA array
- The method new is the constructor
- Example
- use CGI
- cgi new CGI instantiate
- call an instance method
- nom cgi-gtparam(Nom)
- call a class method
- CGI-gtnph(1)
12PERL In-liners
13In-liners some options
- -a autosplit (only with -n or -p)
- -c check syntax
- -d debugger
- -e pass script lines
- -h help
- -i direct editing of a file
- -n loop without print
- -p loop with print
- -v version
- Example
- perl -e 'print qq(hello world\n)'
14In-liners -n and -p
- perl -pe s/\r/\n/g ltfilegt
- is equivalent to
- open READ, file
- while (ltREADgt)
- s/\r/\n/g
- print
-
- close(READ)
- perl -i -pe s/\r/\n/g ltfilegt
- The -i option modifies the file directly
- perl -ne is the same without the print
15In-liners -a (only with -n or -p)
- perl -ane print _at_F, \n ltfilegt
- is equivalent to
- open READ, file
- while (ltREADgt)
- _at_F split( )
- print _at_F, \n
-
- close(READ)
- Example
- hits -b 'sw' -o pff2 prfCARD perl -ane 'print
join("\t", reverse(_at_F)),"\n"'
16In-liners -a (only with -n or -p)
- hits -b 'sw' -o pff2 prfCARD
- swICEA_XENLA 1 90 prfCARD 5
-1 18.553 - swRIK2_MOUSE 435 513 prfCARD 5
-11 15.058 - swCARC_HUMAN 1 88 prfCARD 6
-1 15.395 - swNAL1_HUMAN 1380 1463 prfCARD 7
-1 15.058 - swASC_HUMAN 113 195 prfCARD 7
-2 15.374 - swCAR8_HUMAN 347 430 prfCARD 8
-1 18.343 - swCARF_HUMAN 134 218 prfCARD 9
-1 12.932
- hits -b 'sw' -o pff2 prfCARD perl -ane 'print
join("\t", reverse(_at_F)),"\n"'
18.553 -1 5 prfCARD 90 1
swICEA_XENLA 15.058 -11 5
prfCARD 513 435
swRIK2_MOUSE 15.395 -1 6 prfCARD
88 1 swCARC_HUMAN 15.058 -1
7 prfCARD 1463 1380
swNAL1_HUMAN 15.374 -2 7 prfCARD
195 113 swASC_HUMAN 18.343 -1
8 prfCARD 430 347
swCAR8_HUMAN 12.932 -1 9 prfCARD
218 134 swCARF_HUMAN
17In-liners examples
- perl -e print int(rand(100)),"\n" for 1..100'
perl -e 'x_1 while ltgtprint sort altgtb
keys x'
- for(i0ilt100i)
- nb int(rand(100))
- hashnb 1
-
- print sort altgtb keys hash
18In-liners extract FASTA from SP
- open (READ, /db/proteome/ECOLI.dat) open
file - while (lineltREADgt) read line by line until
the end - if(line /ID (\w)/) print gt1\n
print fasta header - if(line / /)
- line s/ //g remove spaces
- print line print sequence line
-
-
- close(READ)
- cat /db/proteome/ECOLI.dat perl -ne if (/ID
(\w)/) print"gt1\n" if(/ /) s/ //g print
19In-liners your turn
- Create an In-liner that extracts non-redundant
FASTA format sequences from a redundant database
in SwissProt format
- cat /db/proteome/ECOLI.dat perl -ne ' if (/ID
(\w)/) print "gt1\n if(/ /) s/ //g
print' perl -e 'while(ltgt) if (/gt/) i_
xi"" xi._ print values x
20Exercises 4.1 and 4.2
- Create a Perl module MyHTML.pm containing 4
functions - htmlHeader prints the HTTP header and
ltHTMLgtltBODYgt tags - htmlFooter prints lt/BODYgt and lt/HTMLgt tags and
close - htmlError calls htmlHeader, prints an error
message, calls htmlFooter - addLink gets a string, check for links, add
them, return modified string (see exercise 3.1) - Modify script of Exercise 3.1 by removing the
internal function calls and replace them by calls
to the module MyHTML.pm. - Create a Perl In-liner that reads a database in
SwissProt format and creates a non-redundant list
of all the keywords (from KW lines). - Optional count each occurence of the same
keyword...