How can I export a function named "import" from a module in Perl?

I wrote a special import function that will be used in several places, and I would just like to use "Use ImportRenamer"; in these modules and use them later for import from ImportRenamer. How can i do this?

Edit: In other words: How to import an "import" without starting it?

+4
source share
4 answers

UPDATE

Now that the OP has clarified its needs, it really needs to be done just like what Exported does, to be precise, by entering an auxiliary link into the caller's namespace by assigning glob. 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 RESPONSE

You do not need to do anything special except call the import method " import ". use already calls import() , see perldoc use :

 use Module LIST 

Imports some semantics into the current package from a named module, usually by smoothing certain routines or variable names into your package.

This is exactly equivalent to:

  BEGIN { require Module; Module->import( LIST ); } 
+3
source

You need to write your own import method in your module, which will export your function that you want to import in the calling namespace.

Here is some untested code:

 package ImportMe; sub import { # Get package name my $caller = caller; # Install my_import into the calling package as import { no strict 'refs'; *{"${caller}::import"} = \&my_import; } return 1; } # renamed as import when installing sub my_import { # do stuff } 

Now you can put use ImportMe; into all your modules, and the import method will be installed.

+3
source

Alternatively, you can avoid the offset in the character table with Sub :: Exporter

 package Your::Module; use Sub::Exporter ( -setup => { exports => { import => sub { return \&_real_import } }, # the "default" group auto-exports "import" without the caller # specifically asking for it groups => { default => [qw(import)] }, } ); sub _real_import { ... } 

EDIT: Added default group for auto export

+3
source

How about something like this:

 package Your::Module; use strict; use warnings; sub import { # gets called by use Your::Module my ( $pkg ) = caller; no strict 'refs'; *{ $pkg . '::import' } = \&_real_import; } sub _real_import { # import function to be exported to caller print "blah blah"; } 

This manually assigns _real_import sub to the import word of the namespace of the calling package. If you do this in a BEGIN block, then the import subtext must be ready and wait for this package to receive use d.

+2
source

All Articles