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

I was using a module Project::Config without problems, which had this structure:

package Project::Config; use warnings FATAL => qw(all); use strict; use Exporter; our @ISA='Exporter'; our @EXPORT_OK=qw(....); ... sub AUTOLOAD {...} INIT {...}
In one application, I wanted to replace the
use Project::Conifg;
by an explicit BEGIN { require .... import .... } sequence, and I wrote it like this:

package MyApp; use strict; use warnings; # This had worked before: # use Project::Config qw(foo bar); # New code which does not work: BEGIN { require 'Project/Config.pm'; Project::Config::import(qw(foo bar)); }
It turned out that the call of the import function triggered Project::Config::AUTOLOAD(), i.e. as if no import were defined. I had expected that by compiling Config.pm, the Exporter would inject an import() method. Any idea why this failed?
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: import not found
by tilly (Archbishop) on Jan 27, 2011 at 15:18 UTC
    Exporter does not inject a function, instead you inherit from it and the method call to import is supposed to resolve. When you call Project::Config::import(qw(foo bar)); you're not making a method call, so you don't search your inheritance tree and find Exporter. Change that line to Project::Config->import(qwfoo bar)); and things should improve.

    Incidentally most things people do with AUTOLOAD are better done by other means. (In my experience, usually by assigning a bunch of closures to functions into the current package.) AUTOLOAD is a sledgehammer that can make code more brittle than it should be. With no context I don't know whether that is true in your case.

      Exporter does not inject a function

      It does if you use

      use Exporter qw( import );

      instead of

      use Exporter qw( ); our @ISA = 'Exporter';

      But you still need to call it as a method.

      Thanks a lot for the explanation!
      AUTOLOAD is a sledgehammer that can make code more brittle than it should be. With no context I don't know whether that is true in your case.
      I start to believe that it is true in my case...

      -- 
      Ronald Fischer <ynnor@mm.st>
        Incidentally a random tip. If you want Exporter to inject a function into your space you can
        use Exporter qw(import);
        rather than inheriting from Exporter.
Re: import not found
by Anonyrnous Monk (Hermit) on Jan 27, 2011 at 15:17 UTC
    I had expected that by compiling Config.pm, the Exporter would inject an import() method

    It doesn't.  When you use @ISA='Exporter', the method would be looked up via inheritance.  But you can alternatively directly import Exporter's import method into your package:

    use Exporter 'import';

    Also, you probably wanted to call import as a method, i.e. using the arrow notation:

    Project::Config->import(qw(foo bar));
Re: import not found
by JavaFan (Canon) on Jan 27, 2011 at 15:11 UTC
    The right way would be to call
    Project::Config->import(qw(foo bar));