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

Hi All,
   I've got a loop which loads the LWP module only if it's needed. The code I use to load the module is:-

sub LoadModule { my $module = $_[0]; my $loadok = 1; package main; eval "require $module"; if ($@) { package CosmicSub; $loadok = 0; package main; } ## End if else { $module->import(@_[1 .. $#_]); } ## End else package CosmicSub; return $loadok; } ## End sub

How do I know if the module has already been loaded so I don't end up reloading it each time the conditions arise in the loop?
I'm calling the routine from all sorts of different packages so setting a variable wouldn't be the best solution.

Once again thanks!

Replies are listed 'Best First'.
Re: Seeing if a module is loaded
by davidrw (Prior) on Jul 13, 2005 at 02:22 UTC
    require won't reload the module.. it only loads it once. take a look at perldoc -f require: demands that a library file be included if it hasn't already been included.

    But if you wanted to check, do what require does (see the docs) and check for $INC{$filename};