Download presentation
Presentation is loading. Please wait.
1
Perl Modules
2
Perl Modules Namespaces are the mechanism using which you will create modules, libraries, classes and objects. Name spaces are places to segregate code so that one segment of code does not conflict with another – functions and variables “live” in a name space. A Package is a group of perl code that exists within its own namespace. A module is a reusable package placed in a separate file under the same name as the package name and a.pm extension. Perl implements a class using a package, but the presence of a package doesn't imply the presence of a class. A class is a package that provides subroutines that can be used as methods. Modules are included into scripts via use module. Libraries are similar to modules, but are included in the application while the program is running. One namespace per file and one file per namespace.
3
Some well-known modules
CGI - web programming CGI.pm is the most used module for CGI scripts. CGI.pm makes programmer's life easy, without making him bother much about the HTML tags . DBI - common database interface DBI is a database access Application Programming Interface (API) for the Perl Language. The DBI API Specification defines a set of functions, variables and conventions that provide a consistent database interface independant of the actual database being used. In simple language, the DBI interface allows users to access multiple database types transparently. The DBI requires one or more driver modules to talk to databases. Oracle, Access and ODBC drivers might be of interest to us. GUI - interfaces to GUI toolkits Perl also provides modules for various GUI toolkits on Unix like Gtk, Tk, XForms etc. A beta version for Win32 GUI is also available.
4
There are two special subroutines called BEGIN and END that act as constructors and destructors in a package. A BEGIN subroutine is executed as soon as possible even before the rest of the file is parsed. Multiple BEGIN blocks are allowed in a file and execute in the order of definition. An END subroutine is executed as late as possible (when the interpreter is being exited). Multiple END blocks are allowed in a file and execute in the reverse order of definition.
5
use Modules are included into scripts via use module.
All modules that are included in the scripts are syntax-checked and then executed. Example: if ($flag eq ‘On’){ use ModuleOne; } else use ModuleTwo; Will translate into if ($flag eq ‘On’){ } else {}
6
To write a Simple module Two ways
Simple.pm package Simple; sub True { return (1); } sub False { return (0); } 1; Client.pl use Simple; print Simple::True(); Simple.pm package Simple; use Exporter; @ISA = qw(Exporter); @EXPORT = qw (True); sub True { return (1); } sub False { return (0); } 1; Client.pl use Simple; print True();
7
Modules with use. Write a perl script that has a .pm suffix, for example, Employee.pm. The package name is Employee in the Employee.pm. To use Employee.pm in a program called EmployeeDemo, include use Employee Use is done at compile time.
8
Employee.pm package Employee Sub new { } EmployeeDemo.pl use Employee; $emp1 = Employee->new();
9
Objects package Student; #in file Student.pm
sub new { # A constructor that returns a blessed # reference. my $self = {}; #the object is created as an #anonymous hash $self->{NAME} = undef; $self->{MAJOR} = undef; # bless makes and returns the object to the function # that called it. bless($self); return $self; } sub name { my $self = shift; if { $self->{NAME} = shift } return $self->{NAME}; } sub major { if { $self->{MAJOR} = shift } return $self->{MAJOR}; 1;
10
#! /usr/local/bin/perl -w
use Student; $one = Student->new(); #Create one Student object $one->name("Smith"); $one->major("COEN"); printf "Name: %s Major: %s \n", $one->name, $one->major;
11
#!/usr/local/bin/perl -w
package Course; sub new { my($type,$args) #type or class is #always passed as the first parameter my $self = {}; $self->{NAME} = $args; return bless($self,$type); } sub name { my $self = shift; return $self->{NAME}; 1;
12
package Course; sub new { my($type)= shift; my $self = {}; $self->{NAME} = shift; $self->{UNITS} = shift; #the two argument bless should be used if # this class is to be used as a parent class return bless($self,$type); #an alternate way of passing parameters # to bless is #return bless ($self, ref($type) || ($type) }
13
#! /usr/local/bin/perl -w
use Course; $c = Course->new(“COEN276”,4); $d = c->new (“coen176”,4); #the class is derived using the ref() from the object.
14
Inheritance – ISA relationship
#!/usr/local/bin/perl -w package GradCourse; use Course; @ISA = qw(Course); #create base object and extend with new properties sub new { my ($class,$name, $units,$project) my $self = Course->new($name,$units); $self->{PROJECT} = $project; return bless $self, $class;} sub getProject { my $self = shift; return $self->project;} sub name { my $fullTitle = $self->SUPER::name() . "_G"; return $fullTitle; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.