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

I have one module (module A) that requires a file. Then I have another module (module B) that requires the same file.

This required file is not a module. It's just a file containing a bunch of functions.

When module A uses module B and then module A calls a function from that required file, I get an error: The function can't be found.

What's going on?

  • Comment on Why is 'require' not working as I expect?

Replies are listed 'Best First'.
Re: Why is 'require' not working as I expect?
by Corion (Patriarch) on Oct 24, 2005 at 06:48 UTC

    Any required file only gets loaded once, see perldoc -f require. So your file gets only loaded once. I recommend putting your required file into a module and using that from both packages. You could also load it with do $file.

      To clarify Corion's point: use() is expanded to this:

      BEGIN { require Module; import Module LIST; }

      So unlike a plain require() (which does only load the file once), use() calls import() each time, which is responsible for making the functions available in your code where you need it.

      "Turning your required file into a module" means giving it an import() routine that makes functions available in whatever package you happen to be in when use() is called. That is typically done with Exporter. use() also takes a bareword -- not a file name, so you have to ensure the file name and barewords are in sync.

      So, for example, in the file "utility.pm":

      package utility; # package "utility" is sought as "utility.pm" use base 'Exporter'; # inherit the "import" sub @utility::EXPORT = qw( wibble wobble ); # tell "import" what subs to e +xport sub wibble { print "wibble" } sub wobble { print "wobble" } 1; # modules must return a true value

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      It is still not working when I put the code into a module.

      I get the same error: undefined subroutine.

      So to clarify, now I have module A which uses module B. Both modules use module C which exports many functions by default. When module A tries to call one of these functions, I get the error.

      Thanks for the help.

        Did you read the documentation on require? Did you read xdgs excellent explanation which he posted to further clarify my reply? Did you read the other reply? What part did you not understand about the explanations given?

Re: Why is 'require' not working as I expect?
by Moron (Curate) on Oct 24, 2005 at 11:41 UTC
    The function can't be found because it becomes loaded into the namespace of either module A or module B, depending on a fact you as yet haven't revealed, this being whether:

    a) module A uses module B before it does the require (in this case the function becomes loaded into B's namespace) or

    b) module A requires the file before using module B (and in this case the function becomes loaded into A's namespace).

    -M

    Free your mind