Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Module Usage

by Lord Rau (Novice)
on Oct 13, 2000 at 19:33 UTC ( #36600=perlquestion: print w/replies, xml ) Need Help??

Lord Rau has asked for the wisdom of the Perl Monks concerning the following question:

Do you need to determine if a module has already been "use"d? Or can you "use" with impunity? Does just re"use"ing it have any detriments on memory?

Replies are listed 'Best First'.
Re: Module Usage
by merlyn (Sage) on Oct 13, 2000 at 19:42 UTC
    use is based on require, which does a once-only check. However, the import is done separately at each use, but recall that use is also built on BEGIN, so it's performed once at compile-time, not run-time.

    -- Randal L. Schwartz, Perl hacker

      Ok? So that sort of answers my question...
Re: Module Usage
by extremely (Priest) on Oct 14, 2000 at 03:20 UTC

    The short form of merlyn's answer is "use with impunity" =)

    His point was that the "use Xxxx" happens during the "compile" stage, before the code begins to run. See use vs. require and use are helpful.

    Oddly, the opposite topic came up just a week ago or so. Someone wanted to know HOW to make a "use" not happen at compile time. Here is one post from May Detecting modules in use? and the post from Oct 9, loading a module only if you need it.

    Hope that helps...

    $you = new YOU;
    honk() if $you->love(perl)

RE: Module Usage
by Anonymous Monk on Oct 15, 2000 at 11:17 UTC
    ...because you typed 'use ThisModule;' at the top of your code is the simple answer -- since use'ing occurs at compile-time, then the statement must already occur in your code. You cannot load a module twice, if it has already been loaded with use or require. This also means that you can't have two different versions of the same module loaded at one time either.

    if you are talking dynamic inclusion of module at runtime, or in response to user responses etc, then the perl idiom you are looking for is:

    if ( $i_need_a_dynamically_loaded_module ) { eval "require Module"; $@ && warn $@; }

    if you have a list of modules which could do the job but you don't know what the host has, then either traverse @INC manually (clumsy), or try something like:

    my $loaded_mod; foreach my $mod ( qw( Module1 Module2 Module3 ) ) { warn "attempting to load '$mod'"; eval "require $mod"; next if $@; warn "'$mod' loaded successfully"; $loaded_mod = $mod; last; } $loaded_mod or die "Couldn't load anything useful..."; ...


RE: Module Usage
by AgentM (Curate) on Oct 13, 2000 at 21:18 UTC
    What you cannot do is try to unload a module and then try reloading since the no will try a an unimport within the unloaded module. bad idea. I tried using CGI twice and i got no errors. My memory usage indicator does not show such a low resolution of memory blocks os I can't tell you if it's actually loaded twice. great question ++. ps -aux anyone?
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://36600]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-09-26 07:25 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (117 votes). Check out past polls.