0

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?

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
Andreas
  • 1,211
  • 1
  • 10
  • 21

1 Answers1

1

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?

tjd
  • 4,064
  • 1
  • 24
  • 34
  • I am exporting the `hello` function using `Exporter`. But you helped me, I was not using `package` in `MyB`. It works now, thanks. – Andreas Mar 19 '13 at 13:58
  • That's interesting. I wonder what `*Main::hello` looks like after you `import` `&Main::hello` into it's own namespace. I can't imagine it's good. Fodder for another day.... – tjd Mar 19 '13 at 16:55