Reputation: 1008
I need to convert a 'use' statement in my Perl code to 'require' statement to import some %EXPORT_TAGS from another module. The module that I am "use"ing is a plain .pm file (not a class with constructor).
Below is the snippet of the MyModule.pm file
package MyModule;
use Exporter qw(import);
our %EXPORT_TAGS = ( 'tag1' => [qw (func1 func2)],
'tag2' => [qw (func3 func4)]
);
our @EXPORT_OK = qw (func1 func2 func3 func4);
sub func1 {
#some statement
}
# similary other functions written ...
The "use" statement that I have tried from my script looks like below:
use MyModule qw(:tag1 :tag2);
Now I need to convert it to a "require" statement. I tried what is suggested here as below but of no help :
require MyModule;
MyModule->import;
One Limitation, that is : This MyModule.pm is used in other scripts by "use" statement so I cannot remove %EXPORT_TAGS and @EXPORT_OK;
Any help to write me the correct and working "require" syntax will be highly appreciated.
Thanks in advance.
Upvotes: 2
Views: 143
Reputation: 132914
This use
statement:
use MyModule qw(:tag1 :tag2);
is this require
:
BEGIN {
require MyModule;
MyModule->import( qw(:tag1 :tag2) );
}
Without the BEGIN
, the module isn't loaded until Perl encounters that statement during run time, which might be too late for the code that wants to use the things you want to import.
If you explained why you need require
, we could probably clear up that problem.
Upvotes: 1
Reputation: 54381
The use
statement basically translates to a BEGIN
block with an import
call, where the argument list is passed into import
.
So this would translate to
require MyModule;
MyModule->import(qw/ :tag1 :tag2 /);
It doesn't matter if it's tags, or function names, or variable names. The only special case is if you have use Foo ()
with an empty argument list, in which case there will not be an import
call at all.
Upvotes: 3