These are the things which have to be included in the module from where you need to export:
use Exporter.
@ISA=qw(Exporter);
@EXPORT = qw(add subtract);
@EXPORT_OK=qw(multiply divide);
Here use Exporter loads the module into the user namespace at the compile time.
@ISA is similar to the inheritance.
Here we add Exporter to the @ISA array.
@EXPORT contains the list of symbols which are exported into the user's namespace.
@EXPORT_OK contains the list of symbols which can be exported if requested by the user.
How really the export takes place:
For example:
consider the module example.pm:
use Example;
use Exporter.
@ISA=qw(Exporter);
@EXPORT = qw(add subtract);
@EXPORT_OK=qw(multiply divide);
sub add{
($no1,$no2)=@_;
$no3=$no1+$no2;
return $no3;
}
sub subtract
{
($no1,$no2)=@_;
$no3=$no1-$no2;
return $no3;
}
sub multiply
{
($no1,$no2)=@_;
$no3=$no1*$no2;
return $no3;
}
sub divide
{
($no1,$no2)=@_;
$no3=$no1/$no2;
return $no3;
}
1;# this important to signify the end of the package.Let us use this module in another file sample.pl:
Content of sample.pl to use Export Module:
# in this case only the default subroutines add and subtract will be added to the user's namespace
use Example;#But when you need multiply and divide subroutines you need to specify as these are available only on #demand basis in @EXPORT_OK.
use Example qw(multiply divide);# once it is imported into the namespace you can directly use it
$ss=add(10,50);print "addition results are:$ss";
#In similar way all the subroutines can be called
How this works:
When the sample.pl loads the module Example.pm the import module in the Example.pm is called automatically making the functions and variables to be added into the user's namespace.
No comments:
Post a Comment