Pm2

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Pm2 as PDF for free.

More details

  • Words: 1,077
  • Pages: 8
Perl Modules

Perl Modules Maharajan M Vinai AR

Telecom Service Provider Division Wipro Technologies

Wipro Technologies

Page 1 of 8

Perl Modules

Table of Contents ............................................................................................................................................1 Table of Contents................................................................................................................2 Introduction to PERL Modules..........................................................................................3 What is PERL Module?........................................................................................ ....................3 Why PERL Modules?.............................................................................................................. ..3 Module Structure .............................................................................................. .......................3 Sample PERL Module......................................................................................................... ......6

Wipro Technologies

Page 2 of 8

Perl Modules

Introduction to PERL Modules What is PERL Module? PERL Module is a file containing related functions designed to be used by programs and other modules. Every module has a set of variables and functions that can be used by the external programs and modules. Why PERL Modules?  Reusable functions can be grouped together and can be imported during compile time.  Required symbols (functions and variables) alone can be imported.  Different levels of public interfaces can be provided using EXPORT, EXPORT_OK and different tags.  By assigning package name to the module, the identifiers/symbols can be associated with a global namespace. Module Structure Structure of any PERL module package <Module name>; use strict; our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); use Exporter; $VERSION = ; @ISA = qw(Exporter); @EXPORT = qw(…); # Symbols to autoexport (:DEFAULT tag) @EXPORT_OK = qw(…);# Symbols to export on request %EXPORT_TAGS = ( TAG1 => […], TAG2 => […], … ); ################### # your code goes here ################### 1;

# This should be the last line.

Package Name:

Wipro Technologies

Page 3 of 8

Perl Modules

The first line of the code has the package name. Usually the package name and the file name are same. The package name is used to allocate a namespace for the symbols used in the module, so that they won’t interfere with the symbols used in the programs that import this module. E.g.: For a PERL Module Sample, the file name would be Sample.pm and the package name would be Sample. package Sample; The above module can be imported in other PERL programs as below use Sample; If the file Sample.pm is placed under a directory Wipro then the package name should also have the directory name where the file is placed. package Wipro::Sample; To load the above module the use command should be as below use Wipro::Sample; Exporter: The command use Exporter; is used to load the Exporter Module, which manages the modules public interface. Public interface is nothing but the symbols designed for use by other PERL programs and modules. @ISA: The special, per-package array @ISA contains the word “Exporter”. When a user says use Wipro::Sample; perl implicitly calls a special method, Wipro::Sample->import ( ). We don’t have an import method in our package, but that’s okay, because the Exporter package does and we are inheriting from it because of the assignment to @ISA. Perl looks at the package’s @ISA for resolution of undefined methods. $VERSION: When a module is loaded, a minimal required version number can be supplied. If the version isn’t at least high, the use will raise an exception. use <Module name> 2.0 # If $VERSION < 2.0, fail @EXPORT: This array contains the list of functions and variables that will be exported into the caller’s own namespace so they can be accessed without being fully qualified. Typically, a qw( ) list is used.

Wipro Technologies

Page 4 of 8

Perl Modules

E.g.: @EXPORT = qw(Func1 Func2 @ArrayList); With the simple use <Module name> call, The function Func1 can be called as Func1 ( ) rather than <Module name>::Func1 ( )  The array @ArrayList can be accessed as @ArrayList instead of @<Module name>::ArrayList 

To load a module at compile time but request that no symbols be exported, use the special form use Exporter ( ), with empty parenthesis. @EXPORT_OK: This variable is used to list the symbols that are to be loaded on special requests. E.g: @EXPORT_OK = qw( SpclFunc $SpclVar); The user can load the module like below use <Module name> qw(SpclFunc $SpclVar Func1) and import only the SplcFunc function, the SplcVar scalar variable and the Func1 function. The function Func1 is already listed in the @EXPORT array. The other symbols Func2 function and @ArrayList array listed in @EXPORT array are not imported automatically. The user can also load the module like below use <Module name> qw(:DEFAULT $SpclVar); to import all the symbols listed in @EXPORT array and $SpclVar listed in @EXPORT_OK array. %EXPORT_TAGS: This tag is for creating higher level groupings of related import symbols. The values of these tags are references to arrays of symbol names that are already listed in either @EXPORT or @EXPORT_OK array. E.g.: %EXPORT_TAGS = ( AllFunctions => [ qw(Func1 Func2 SpclFunc) ], AllVariables => [ qw(@ArrayList $SpclVar) ] );

Wipro Technologies

Page 5 of 8

Perl Modules

An import symbol with a leading colon means to import a whole of symbols. E.g.: use <Module name> qw( :AllFunctions $SpclVar); The above use will import all the functions (Func1, Func2 and SpclFunc) that are listed against the AllFunctions tag and the variable SpclVar. The :DEFAULT tag means all the symbols listed in the @EXPORT array. This module can be called from other PERL files using any of the following commands: use <Module name>; # Import default symbols use <Module name> qw(…); # Import listed symbols use <Module name> ( ); # Do not import any symbols use <Module name> qw(:TAG1); # Import the whole tag set

Sample PERL Module Below is a very simple PERL Module used for integer arithmetic operations like Addition, Subtraction, Multiplication and Division. #!/usr/bin/perl –w package Arithmetic; use strict; our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); use Exporter; $VERSION = 1.0; @ISA = qw(Exporter); @EXPORT = qw(Add Subtract Multiply Divide ); sub Add { my ($val1, $val2) = @_; my ($result); if($val1 !~ /^\d+$/ || $val2 !~ /^\d+$/) { return undef; } $result = $val1 + $val2;

Wipro Technologies

Page 6 of 8

Perl Modules

return $result; } sub Subtract { my ($val1, $val2) = @_; my ($result); if($val1 !~ /^\d+$/ || $val2 !~ /^\d+$/) { return undef; } $result = $val1 - $val2; return $result; } sub Multiply { my ($val1, $val2) = @_; my ($result); if($val1 !~ /^\d+$/ || $val2 !~ /^\d+$/) { return undef; } $result = $val1 * $val2; return $result; } sub Divide { my ($val1, $val2) = @_; my ($result); if($val1 !~ /^\d+$/ || $val2 !~ /^\d+$/) { return undef; } $result = $val1 / $val2;

Wipro Technologies

Page 7 of 8

Perl Modules

return $result; } 1;

Wipro Technologies

Page 8 of 8

Related Documents

Pm2
November 2019 4
Pm2
October 2019 6
Dp-01 > Dev > Pm2
June 2020 2
Pm2-sup (1).docx
April 2020 8
Pm1, Pm2 & Pm3
May 2020 12