perlsOfLondon

 

Intro To Packages

Page history last edited by dmlond 2 yrs ago

Packages were developed in the earliest programming languages as a way of housing commonly used code in one place, to be shared by many different programs. In the UNIX world, these were housed in shared libraries, and windows calls these DLLs. Perl also has packages. Packages can be defined in a script, along with the code that uses it, but they are usually defined in their own special files. Packages have a name (and can be part of a namespace, in the case where there are multiple packages within a group created by the same user, or providing functionality around a common domain, etc.).

They also define a set of variables (scalars, arrays, hashes) which are to be 'exported' to the code which uses them. This allows packages to contain variables and subroutines which are for internal use only, and not visible to the calling program (referred the 'user'). In order to make use of a package in your perl script, you simply 'use' it. Some commonly used Perl Packages:

  • Cwd = allow code to efficiently determine the current working directory of the script.
  • File::Basename = get the basename (filename) or dirname (everything but the filename) from a (possibly) fully specified path to the file.
  • File::Find = recursively search a directory for files fitting a search criteria that you program, and manipulate these files in perl.
  • Date::Manip (May not be installed by default, search CPAN) = compare two dates, calculate different date formats, etc.
CPAN : Comprehensive Perl Archive Network. Online, searchable repository for millions of useful Perl Modules (Packages and Objects). If you need to do something in perl, before writing your own module or package, see if there isnt already something that does it on CPAN.

Writing your own. All perl packages (unless they are object oriented), need to define their package name, use Exporter, and tell perl that they are extending the Exporter package. This is done with the following four lines of code placed at the top of the code:

package MYPACKAGE;

use Exporter;

use vars qw(@ISA @ISA @EXPORT @EXPORT_OK other variables that you want to export also go here);

@ISA = qw(Exporter);

The package name must end with the name of the perl file it is inhabiting (e.g. if the package is called MYPACKAGE, the file must be called MYPACKAGE.pm. Spelling and case are important). If it is part of a namespace, e.g. SOME::NAME::MYPACKAGE, then it must be in the SOME/NAME directory relative to one of the directories specified in @INC. Otherwise, the Module file itself (e.g. MYPACKAGE.pm) must itself be in one of these directories.

Finally, the package must tell perl which variables and subroutines it wants to EXPORT. This is done using one of the three following ways.

  1. @EXPORT - everything placed in this array will be exported by default.
  1. @EXPORT_OK - everything placed in this array will be exported only if requested specifically in the use request (e.g. use PACKAGE qw(things you want specifically go here);).

Comments (0)

You don't have permission to comment on this page.