Search code examples
perlperl-module

Use (include/require) the same file twice


I have a perl script (MyTest.pl) that includes (use) two modules (MyA.pm and MyB.pm). The problem I have is that module A also have to include Module B, but this doesn't seem to work as is already has been included in the .pl file.

MyTest.pl

use MyA;
use MyB;
print hello(); # defined in MyB

MyA.pm

use MyB;
print hello(); # defined in MyB

perl states that the subroutine hello is undefined when called from MyA.pm. From what I can grasp it seems like the use only works where it is used (ha!) the first time.

Any clues?


Solution

  • You don't show us an import method for MyB or tell us if it inherits the import method of a standard module like Exporter. Without an import method, the MyB::hello subroutine can't be aliased into the Main or MyA namespaces.

    Your 2 choices are to do the import or to use the full name of MyB::hello.

    Edit: Hmm, I also notice that you're not using a package name in MyA. Are your modules all using the Main namespace as their personal litter box?