UPDATE
Now that OP clarified his needs, this should indeed be done in a way similar to what Exported does, to be precise, by injecting the sub reference into a caller's namespace via a glob assignment. Example:
###############################################
package ImportRenamer;
use strict;
sub import_me {
print "I am a cool importer\n";
}
sub import {
my ($callpkg)=caller(0);
print "Setting ${callpkg}::import to ImportRenamer::import_me\n";
no strict "refs";
*{$callpkg."::import"} = \&ImportRenamer::import_me; # Work happens here!!!
use strict "refs";
}
1;
###############################################
package My;
use strict;
use ImportRenamer;
1;
###############################################
package My2;
use strict;
use ImportRenamer;
1;
###############################################
And the test:
> perl -e '{ package main; use My; use My2; 1;}'
Setting My::import to ImportRenamer::import_me
I am a cool importer
Setting My2::import to ImportRenamer::import_me
I am a cool importer
ORIGINAL ANSWER
You don't need to do anything special beyond calling the import method "import
". use
already calls import()
, see perldoc use:
use Module LIST
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your package.
It is exactly equivalent to:
BEGIN { require Module; Module->import( LIST ); }