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

Hi,

A long time ago I wrote a module which seems to work fine. It is a bit like this, but does more.

# OddEffectOfImportTest.pm v1 package OddEffectOfImportTest; use Exporter; @ISA = qw(Exporter); @EXPORT = qw($OddEffectOfImportTest); our $OddEffectOfImportTest = 6.28; 1;

... and using it like this is fine.

#!/usr/bin/perl # OddEffectOfImportTest v1 use strict; use OddEffectOfImportTest; print "$OddEffectOfImportTest\n";

the point being that $OddEffectOfImportTest doesn't have a package name.

Now I've decided it would be useful to help it initalise, so I can use it thus:

#!/usr/bin/perl # OddEffectOfImportTest v2 use strict; use OddEffectOfImportTest goreallyfast => 1; print "$OddEffectOfImportTest\n";

but when I add "sub import{}" to the module:

# OddEffectOfImportTest.pm v2 package OddEffectOfImportTest; use Exporter; @ISA = qw(Exporter); @EXPORT = qw($OddEffectOfImportTest); our $OddEffectOfImportTest = 6.28; sub import {} 1;

..., without any other changes, I now I get

Global symbol "$OddEffectOfImportTest" requires explicit package name +at ./OddEffectOfImportTest line 5. Execution of ./OddEffectOfImportTest aborted due to compilation errors +.

Lots of my scripts use this module and I rather not track down and change them all if possible.

Threads 277141 and 569862 looked related but not close enough (for me to understand, at least.

Thanks for any help.

  • Comment on Adding "sub import{}" to my module makes global symbols require an explicit package name
  • Select or Download Code

Replies are listed 'Best First'.
Re: Adding "sub import{}" to my module makes global symbols require an explicit package name
by Corion (Patriarch) on Sep 28, 2010 at 16:21 UTC

    Your sub import should call Exporter's sub import, because Exporter works its magic through its own sub import. If you don't do that, exporting symbols does not work. Either export the symbols yourself, or call Exporter.

Re: Adding "sub import{}" to my module makes global symbols require an explicit package name
by afoken (Chancellor) on Sep 28, 2010 at 21:04 UTC

    In addition to what Corion said: With recent perl/Exporter versions (perl >=5.8.3 / Exporter >=5.57), you no longer need to inherit from Exporter, it is sufficient to import Exporter's import routine (see Exporting without inheriting from Exporter). So you can drop the line ...

    @ISA=qw(Exporter);
    ... and change ...
    use Exporter;
    ... to ...
    use Exporter qw( import );

    Of course, if you have your own import method, that won't work. You can use export_to_level instead, see Exporting without using Exporter's import method. Inheriting is also not needed then, and you also don't have to (and should not) import Exporter's import.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thank you both for your replies. I now have it working with export_to_level, though not using it exactly as you described.

      While my use statement was:

      use OddEffectOfImportTest;

      the following worked fine.

      OddEffectOfImportTest->export_to_level(1, @_);

      ... but changing the use statement to:

      use OddEffectOfImportTest xxx => 6;

      ... makes it fail, albeit with a different error.

      All of the next three work

      OddEffectOfImportTest->export_to_level(1);

      OddEffectOfImportTest->export_to_level(1, $_[0]);

      OddEffectOfImportTest->export_to_level(1, $_[0], @EXPORT);

      I plan to use the second one.

      If you think I'm missing something fundamental, please do follow up. Otherwise thank you for your invaluable help.

      Barrie