in reply to Why is 'require' not working as I expect?

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.

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

Replies are listed 'Best First'.
Re^2: Why is 'require' not working as I expect?
by xdg (Monsignor) on Oct 24, 2005 at 13:39 UTC

    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.

Re^2: Why is 'require' not working as I expect?
by Anonymous Monk on Oct 25, 2005 at 20:19 UTC
    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?

        What part did you not understand about the explanations given?

        I understood and implemented them. But I think I tracked down the real problem. Forget what I said about Module A, B and C for a second. Let me explain it anew:

        1. Module A defines some functions that Module B calls. Module A exports these functions into Module B's namespace.

        2. Module B only has one function and it is called by Module A in one of its functions. So I made Modules A use Module B.

        3. When I use warnings with both Module A and B, I get 'subroutine redefined' warnings...

        Do you see my error yet? I think I do. But I am unsure how to solve it. Both modules are interdependant on each other.