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;
}
@_ = ($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:
On Friday, 6 November 2020 at 16:05:04 UTC, Rainer Weikusat wrote:
The Exporter import exports symbols into the namespace of the callingExcellent; thank you. I ran my test programs under the debugger and,
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:
now that you've explained it, I can understand what I saw happening
there.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 292 |
Nodes: | 16 (2 / 14) |
Uptime: | 192:58:48 |
Calls: | 6,616 |
Files: | 12,166 |
Messages: | 5,315,306 |