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

I am new to Perl. I wrote a module (MyModule.pm) to do some image manipulation. I do a 'use MyModule' in 2 other modules (say ModuleX and ModuleY) which are part of my program.

When I call a function from MyModule in ModuleX, the system gives me a error saying ModuleX::functionOne undefined, apparently not loading MyModule.

However if I take out the use statement from one of the modules, then it is fine. My module looks like this

package MyModule; use strict; use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(functionOne functionTwo); $VERSION = 1.00; use Image::Magick; sub functionOne { } sub functionTwo { } return 1;
I thought it was OK to 'use' the same module multiple times. I am not sure if I have provided enough here, but can someone explain what might be going wrong here. Thanks.

Replies are listed 'Best First'.
Re: Using a module twice giving a problem
by toolic (Bishop) on Aug 14, 2011 at 17:27 UTC
    I am not sure if I have provided enough here
    I am sure you are not providing enough detail for us to help. Show your relevant code for ModuleX and Y. Are they in different .pm files? Are you 'use'ing X from Y, or vice versa? Post the exact error message you receive. Show how you run your code to generate the error message. Meanwhile, read use and Exporter.
Re: Using a module twice giving a problem
by happy.barney (Friar) on Aug 14, 2011 at 16:36 UTC
    you didn't show how you "use" module. It looks to me that you have something like:
    use MyModule ();
    In this case, all exports are disabled.
      I just do the following:
      use MyModule;
      The system does find the functions when I comment out one of the 'use', so the functions must be getting exported?
        Please see How do I post a question effectively? and provide us with code that reproduces your problem -- this is basic perl feature, available for over 14 years, its not likely the problem is with perl.
Re: Using a module twice giving a problem
by ikegami (Patriarch) on Aug 15, 2011 at 02:42 UTC

    There is no problem is what you presented to us.

    Do you perhaps change package at some point?

    package One; ... use MyModule; ... package Two; ... functionOne() ...

    &One::functionOne exists, but &Two::functionOne doesn't.

      What you say is perhaps the likely explanation as the module does get loaded. I am weary about the structure of my code and would need to look at it more closely. For now, I am getting around this by explicitly naming the package in the function call (&MyModule::functionOne).

      Does explicitly naming the package name in the function call have its disadvantages?

      Thank you very much for help.

        Does explicitly naming the package name in the function call have its disadvantages?

        Only in that it means you have to do extra typing.
        If calling it that way works but calling it as functionOne produces the error you reported, then that's a clear indication that you've managed to disable the exporting of that function.

        Btw, the general advice is that you should delete the "&" and call the function as MyModule::functionOne

        Cheers,
        Rob

        Some even qualify names when they don't have to, to show the origin of the function.

        You don't actually need the & and in fact shouldn't use it. I wrote "&One::functionOne" as short hand for "sub One::functionOne".

Re: Using a module twice giving a problem
by Anonymous Monk on Aug 15, 2011 at 01:21 UTC
    I would have like to have provided more here so that it would have been easier for people to help. Unfortunately, the code is not very structured and that would not be very helpful.

    I never questioned Perl as I have myself used 'use' multiple times without a problem.

    I thought may be it was the way I was writing the module. I will go back and read up more on 'use' and 'modules' to see if I can find what I am doing wrong.