Henry Law <
[email protected]> writes:
I have a Perl module which is included in numerous programs in the
suite I'm writing. I need to vary the way in which the module is
imported (to do with whether it's to fail or not if certain resources
aren't available), so I want to be able to code
use MyPackage; # When it's to check that the resources are available
use MyPackage nocheck=>1; # When it's not to check
I have written an "import" method in the module which interrogates the parameters (nocheck=>1 in the example); once that's done I need
Exporter's import method to continue to do its thing with exporting subroutines and so forth.
[...]
# ------- MyPackage.pm
package MyPackage;
use strict;
use warnings;
use 5.014;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw/afunction/;
sub import{
my $package_name = shift;
my %parms;
if ( @_ && !(@_ % 2) ){ # Check for even number
%parms = @_;
}
printf "MyPackage::import [parms: %s] invoking Exporter::import %s\n",
(scalar keys %parms), $package_name;
Exporter::import $package_name;
}
The Exporter import exports symbols into the namespace of the calling
package. In your case, that's MyPackage, hence, it's trying to export
the symbol into the package which defines it (symbol is really a Lisp
term and not a Perl term, but ... oh well ...). You have to use goto to
call the subroutine to make the intermediate activation record go
away:
-----------
package MyPackage;
use strict;
use warnings;
use 5.014;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw/afunction/;
sub import{
my $package_name = shift;
my %parms;
if ( @_ && !(@_ % 2) ){ # Check for even number
%parms = @_;
}
printf "MyPackage::import [parms: %s] invoking Exporter::import %s\n",
(scalar keys %parms), $package_name;
@_ = ($package_name);
goto &Exporter::import
}
sub afunction{
print "This is function afunction\n";
}
1;
-------------
(tested with 5.24.1).
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)