Perl’s Exporter/Importer

In this post, I will review how Perl import/export namespaces for modules, routines and parameters.

Let’s see one common line in a .pl file:

use Cn::Gd::Sz::Streets qw(clean_street dirty_street cross_street);

It is equivalent as below:

BEGIN {

require Cn::Gd::Sz::Streets;

Cn::Gd::Sz::Streets->import(qw(clean_street dirty_street cross_street));

}

The BEGIN block runs as the very first step in the .pl file (you can consider it as a init() as other languages). It will check in all of the directories listed in @INC for Cn::Gd::Sz::Streets (sub-directory Cn/Gd/Sz/Streets.pm) and fail in a eval block if that .pm is missing (Note: Streets.pl can not be loaded as it loads .pm extension only). In the BEGIN block Perl automatically calls the import routine to import the modules’ packages. It is the module author’s responsibility to provide appropriate import routines.

So here a tips, if you want to set any $ENV{} parameters into the .pl, you can use the BEGIN{} block, for example, we can use below lines to set the ORACLE_HOME information,

BEGIN{
$ENV{“ORACLE_HOME”} = “E:\Oracle\11.2.0\client_1”;
$ENV{“LD_LIBRARY_PATH”} = “E:\Oracle\11.2.0\client_1\lib”;
$ENV{“NLS_LANG”} = “American_America.AL32UTF8″;
}

So in the following let’s see how Perl’s Export work,

##################################################
package security::cookies;#1
##################################################

use CGI;

use Exporter;#2
@ISA=(‘Exporter’);#3
@EXPORT=(‘$dbsid’,#4
‘$apps’,
‘$sys’,
‘DISCONNECT’,
‘CONNECT’,
‘$validate’);

Line 1 declares the package name as usual, nothing much. In line 2, it requires loading the Exporter which will manage below lines’ public interface. Line 3 initializes a special array @ISA to contain the word :Exporter”. When a user want to load ‘security::cookies’, Perl will implicitly calls a special method security::cookies->import as I described above, however we don’t have an import method in our package, but don’t worry, here @ISA will help it out. @ISA will indicate Perl to look at its resolution of undefined methods. Here It tells Perl to use Exporter to do its Importer stuff (In Perl’s mechanism it is about Inheritance, here we just ignore that and think that @ISA will tell Perl to know how to do the import).

In Line 4, I defined some exported routines and parameters. Those things can be used outside the package (Pls. note, you can not re-declare those parameters using my $xxx in the package or it will overwrite them!)

Below is the example how to use that:

use security::cookies;

CONNECT($paramapps,$paramappspwd,$tnsstring);
my $abc = $apps;

Besides @EXPORT, Perl also provides @EXPORT_OK, from http://www.thegeekstuff.com/2010/06/perl-exporter-examples/

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.

package Arithmetic;

use Exporter;

# base class of this(Arithmetic) module
@ISA = qw(Exporter);

# Exporting the add and subtract routine
@EXPORT = qw(add subtract);
# Exporting the multiply and divide  routine on demand basis.
@EXPORT_OK = qw(multiply divide);

In the above example, we defined the arithmetic module with four functions. By default add() and subtract() functions are exported to the user’s/caller’s namespace.

“use Arithmetic” statement imports the subroutines from Arithmetic module that are exported by default.

“use Arithmetic qw(multiply divide)” indicates that these two routines gets exported only when it is specifically requested as shown in the following code snippet.