Creating a Perl Module
Download
Report
Transcript Creating a Perl Module
Advanced Topics
S517 Session 12, IU-SLIS
1
Reference/Pointer
A reference
►
►
►
►
a pointer to something (e.g. array, hash)
a variable that refer to other variables
contain a memory address of data
good for
•
•
working with large data structures
creating complex data structures
Syntax
►
to create a reference
•
•
►
$ptr1= \$scalar; $ptr2= \@array; $ptr3= \%hash;
$ptr4= [1, 2, 3]; $ptr5= { k1=>v1, k2=>v2}
to get the value
•
Variable (i.e. scalar, array, hash)
•
$$ptr1; @$ptr2; %$ptr3;
Element of array/hash
$$ptr2[0]; $$ptr3{0};
$ptr2->[0]; $ptr3->{0};
S517 Session 12, IU-SLIS
2
Multi-dimensional Arrays
Using anonymous arrays
$array = [ [id1,pw1], [id2,pw2], [id3,pw3] ];
for ($i=0; $i<=2; $i++) {
for ($j=0; $j<=1; $j++) {
print “$array -> [i][j]\n”;
}
Using array references
@a1= (id1,pw1); @a2= (id2,pw2); @a3= (id3,pw3);
@array = (\@a1, \@a2, \@a3); # array of array pointers
for ($i=0; $i<=2; $i++) {
$ptr= $array[$i];
print “@$ptr\n”;
}
Examples: #1, #2, #3, #4, #5
S517 Session 12, IU-SLIS
3
Perl DBI Module
Basics
Connect to a database
►
use DBI;
my $dbh = DBI->connect( DBI:Pg:dbname=$dbname; host=$host; $user, $pw ) ||
die “Can’t connect to DB: $DBI::errstr\n”;
Execute SQL
►
my $sql = “SELECT * FROM $table;
my $sth = $dbh->prepare($sql);
$sth->execute();
Retrieve data
►
while (@row= $sth->fetchrow_array()) {;
print join (“, “,@row),”\n”;
}
►
Disconnect from the database
$dbh->disconnect();
Example Script: #1, #2
Perl DBI Manual
S517 Session 13, SLIS-IU
4
Miscellaneous Features
require “mylib.pl”
►
►
►
►
store and use common subroutines in mylib.pl
looks for mylib.pl in @INC directory
add “return 1” at the end of “mylib.pl”
example: mylib.pl, calling script #1
perlcc prog.pl –o prog.out
►
generates a compiled binary named “prog.out”
S517 Session 13, SLIS-IU
5
Object-oriented Programming
Object
►
►
►
►
►
data structure + functions
knows what it is AND what it can do
belong to some class (i.e. instance of a class)
object characteristics can be inherited
e.g.
animal { var appendage; fn move ( ) }
bird { var wing; fn fly ( ) }
fish { var fin; fn swim ( ) }
Why OOP?
►
►
separate class implementation from class use
code reuse, extension
S517 Session 13, SLIS-IU
6
Object-oriented Perl
Perl5 implements a class using a Package
►
►
►
Defines object and its functions
Created with “package NAME;”
Methods (i.e. subroutines)
•
constructor
•
object methods
►
creates an object of the class
returns a pointer to the anonymous hash where the object data is stored
hash is connected to class by “bless” function
defines functions of the object
the object pointer is stored in $_[0];
Example
S517 Session 13, SLIS-IU
7
Perl Modules (.pm)
Pre-written package for reuse
►
Free Perl5 utilities
•
•
►
e.g. CGI.pm, IO.pm
CPAN, Perl5 Module List
Located in Perl Library directories
•
listed in @INC array (perl –V)
use NAME;
►
►
Perl interpreter looks for NAME.pm in @INC directories
to add $path to @INC
•
•
use lib “$path”;
unshift (@INC, “$path”);
S517 Session 13, SLIS-IU
8
Creating a Perl Module
package Name;
# built-in module w/ Perl Module definition conventions
require Exporter;
@ISA = qw (Exporter);
# make subroutines and variables available to the world
@EXPORT = qw (subname1 subname2);
@EXPORT_OK = qw ($var1 $var2);
sub subname1 { $var1 = ..; }
sub subname2 { $var2 = ..; }
return 1;
Example: Module, calling program
S517 Session 13, SLIS-IU
9
Installing a Local Perl Module
Check for installed module
1.
- find `perl -e 'print "@INC"'` -name '*.pm' -print
2.
put the downloaded module file in a target directory ($DIR)
3.
unpack the file (e.g., gunzip, tar –xvf)
4.
run installation
5.
perl Makefile.PL PREFIX=$DIR
make
make test
make install
add the module path to program
#!/usr/local/bin/perl -w
use lib '/home/mydir/Perl/Modules';
use ModuleName;
S517 Session 13, SLIS-IU
10