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

Howdy,
i've looked everywhere but no answers yet...
if in one script i have the lines
use ModuleA; use ModuleB;
if in ModuleA i ALSO have the line
use ModuleB;
(i.e. one module used twice in 2 separate scripts/packages)
what will happen as regards memory usage/resources?
how many times will ModuleB be compiled?
what memory is assigned? 1 block (block ?) or 2 ?

thanks

Replies are listed 'Best First'.
Re: Multiple uses
by Kanji (Parson) on Mar 27, 2002 at 12:37 UTC
    how many times will ModuleB be compiled?

    Once ... unless you delete it's key/value pair from %INC, which Perl uses to determine if it's already loaded a particular module/file.

    For details, see the "semantics similar to" code example in perldoc require, which I've excerpted the appropriate bits of (plus pointer comments) below for convenience.

    sub require { my($filename) = @_; return 1 if $INC{$filename}; # <-- already loaded? # ... pull in the module $INC{$filename} = $realfilename; # <-- stop future reloads return $result; }

        --k.


Re: Multiple uses
by RMGir (Prior) on Mar 27, 2002 at 12:27 UTC
    Simple test:
    # ModuleA.pm use ModuleB; BEGIN { print "ModuleA::BEGIN\n"; } 1; # ModuleB.pm BEGIN { print "ModuleB::BEGIN\n"; } 1; # main.pl use ModuleA; use ModuleB; BEGIN { print "main::BEGIN\n"; }
    Create those 3 files, then run main.pl.
    $ perl main.pl ModuleB::BEGIN ModuleA::BEGIN main::BEGIN
    It looks like perl does "the right thing".
    --
    Mike