Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Using a module more than once

by bart (Canon)
on Aug 02, 2005 at 15:54 UTC ( [id://480251]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Using a module more than once
in thread Using a module more than once

Or am I misunderstanding how that mechanism works?
You are. Any module that gets used, will be loaded at compile time, thus, when your module is loaded. So there's no use in putting a use statement inside a method.

Sometimes, when some modules are only needed in exceptional circumstances, people can use a require statement in a method or sub, for example to load Carp in an error trapping routine.

You have to be aware of the differences between use and require: require doesn't call import in the loaded modules, and it won't change the allowable syntax for your code either. Thus, subs defined in that module won't be easily recognized as such in the loading script. It's something to look out for.

For OO modules, it makes no difference, whether you choose use or require.

Replies are listed 'Best First'.
Re^4: Using a module more than once
by techra (Pilgrim) on Aug 02, 2005 at 17:04 UTC

    However, as I understand it, you can delay use calls if you are using SelfLoader and then execute the use call inside of a subroutine.

    Example:
    use SelfLoader; sub __PACKAGE__::myfunc; 1; __DATA__ sub myfunc { use ThisOtherModule; }
      Interesting. And it comes with perl 5.8.7, no less. But so does autouse. Its purpose looks similar.
      SelfLoader
      load functions only on demand
      autouse
      postpone load of modules until a function is used

      So... what's the difference between SelfLoader and autouse? And which is "best"?

        I don't know much about autouse, as I haven't tried it out. It looks like autouse's purpose is more context-oriented while SelfLoader is useful in a broader context. The benefit of delaying a 'use Module' command is a side-effect of SelfLoader, while it seems to be the main purpose of autouse.

        SelfLoader delays the compilation of methods/subroutines until they are called, while autouse delays the execution of 'using' a module until it is explicitly called.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://480251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found