Perl Modules - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Perl Modules

Description:

Perl implements a class using a package, but the presence of a package doesn't ... ISA = qw(Course); #create base object and extend with new properties. sub new ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 15
Provided by: ranimikk
Learn more at: https://www.cse.scu.edu
Category:
Tags: class | modules | perl

less

Transcript and Presenter's Notes

Title: Perl Modules


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
  • use ModuleOne
  • use ModuleTwo
  • if (flag eq On)
  • else

6
To write a Simple moduleTwo ways
  • Simple.pm
  • package Simple
  • sub True return (1)
  • sub False return (0)
  • 1
  • Client.pl
  • use Simple
  • print SimpleTrue()
  • Simple.pm
  • package Simple
  • use Exporter
  • _at_ISA qw(Exporter)
  • _at_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-gtnew()

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-gtNAME undef
  • self-gtMAJOR undef
  • bless makes and returns the object to the
    function
  • that called it.
  • bless(self) return self
  • sub name
  • my self shift
  • if (_at__) self-gtNAME shift
  • return self-gtNAME
  • sub major
  • my self shift
  • if (_at__) self-gtMAJOR shift
  • return self-gtMAJOR

10
  • ! /usr/local/bin/perl -w
  • use Student
  • one Student-gtnew() Create one Student object
  • one-gtname("Smith")
  • one-gtmajor("COEN")
  • printf "Name s Major s \n", one-gtname,
    one-gtmajor

11
  • !/usr/local/bin/perl -w
  • package Course
  • sub new
  • my(type,args) _at__ type or class is
  • always passed as the first parameter
  • my self
  • self-gtNAME args
  • return bless(self,type)
  • sub name
  • my self shift
  • return self-gtNAME
  • 1

12
  • package Course
  • sub new
  • my(type) shift
  • my self
  • self-gtNAME shift
  • self-gtUNITS 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-gtnew(COEN276,4)
  • d c-gtnew (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
  • _at_ISA qw(Course)
  • create base object and extend with new
    properties
  • sub new
  • my (class,name, units,project) _at__
  • my self Course-gtnew(name,units)
  • self-gtPROJECT project
  • return bless self, class
  • sub getProject
  • my self shift
  • return self-gtproject
  • sub name
  • my self shift
  • my fullTitle self-gtSUPERname() .
    "_G"
  • return fullTitle
Write a Comment
User Comments (0)
About PowerShow.com