johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, in most of my modules, I've been using the following lines:
use Exporter qw(import); our @ISA = qw(Exporter); our @EXPORT = qw(); our @EXPORT_OK = qw();
This works fine on my linux environment. But when I ran it on windows with ActiveState 5.8.1, it complains about "'import' is not exported by the Exporter module". Looking at the code of Exporter.pm, nothing is explicitly exported in both cases. What's happening here? (I could just remove the qw(import), but that involves change many files, and I'd like to understand the issue here anyway.) thanks.

Replies are listed 'Best First'.
Re: Problem with "use Exporter qw(import);"
by ikegami (Patriarch) on Nov 03, 2004 at 22:43 UTC

    There's no need to import 'import' since you're inheriting:

    use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(); our @EXPORT_OK = qw();

    Alternatively, if you don't like inheriting from Exporter:

    use Exporter; our *import = \&Exporter::import; our @ISA = qw(); # nope, no Exporter our @EXPORT = qw(); our @EXPORT_OK = qw();
Re: Problem with "use Exporter qw(import);"
by tall_man (Parson) on Nov 03, 2004 at 21:23 UTC
    When you say "our @ISA = qw(Exporter);" that means you are inheriting from the Exporter package. Perl will search that package for functions that you don't define for yourself.

    The "use Exporter qw(import);" is asking for the function Exporter::import to be imported to your current package so you can call it as simply "import". But you don't need to import it; you have inherited it. All you need is:

    require Exporter; our @ISA = qw(Exporter);
    By the way, if your @EXPORT and @EXPORT_OK lists are really empty as you show here, there's no point in inheriting from Exporter anyway.

      Using inheritance to get one function is quite overkill. I've had my share of problems from such things. Better is to just import the one import function you want. Unfortunately, old versions of Exporter.pm didn't support the easy way of doing this. I just use:

      require Exporter; *import = \&Exporter::import;

      You can even put those two lines inside of a BEGIN block if you like.

      - tye        

        If I read that I'd have to wonder what bug the author was trying to work around. Your example is pretty non-standard as far as ways of including Exporter go. I hope this doesn't constitute "normal" code for you or that you at least comment why you chose to write it so oddly.
        I'm partial to writing this using goto, which lets us defer the loading of Exporter until we actually need it.
        sub import { require Exporter; goto &Exporter::import }
        I've had my share of problems from such things.
        Could you give an example or two? I've used use base 'Exporter'; for a while; should I be avoiding that?
Re: Problem with "use Exporter qw(import);"
by fergal (Chaplain) on Nov 04, 2004 at 10:16 UTC
    Looking at the code of Exporter.pm, nothing is explicitly exported in both cases. What's happening here?

    Exporter has only been exporting the import routine since 5.8.5 (possibly 5.8.4 but I don't have it handy to check). By explicitly exported do you mean something like @EXPORT_OK = qw( import );? It would have been possible for Exporter to do it that way but actually it's done directly in Exporter's import method.

    If you want to be backwards compatible you need use inheritance or the other suggestions above.