Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How to tell if a package is loaded

by mifflin (Curate)
on May 02, 2011 at 22:42 UTC ( #902578=perlquestion: print w/replies, xml ) Need Help??
mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I need to determine if a package is loaded. The only way I can see to do that is to look through %::

As an example, if I wanted to find out if FindBin was loaded. I could do this...

if (grep { /\AFindBin::\z/ } keys %::) { print "FindBin is loaded\n"; }
Is this the proper way to do this? Is there a better way?


Thanks, this seems to work using %INC

$ cat x use strict; use warnings; use FindBin; use File::Basename; is_loaded('FindBin'); is_loaded('Data::Dumper'); is_loaded('File::Basename'); sub is_loaded { my ($pkg) = @_; (my $file = $pkg) =~ s/::/\//g; $file .= '.pm'; my @loaded = grep { $_ eq $file } keys %INC; print "package $pkg is " . (@loaded ? '' : 'NOT ') . "loaded\n"; return } $ perl x package FindBin is loaded package Data::Dumper is NOT loaded package File::Basename is loaded

Replies are listed 'Best First'.
Re: How to tell if a package is loaded
by John M. Dlugosz (Monsignor) on May 02, 2011 at 22:56 UTC
    I think %INC will show you everything that was pulled in via use or require.

    Yes, this works for me:

    perl -MFile::Find -E'print join "\n", keys %INC'
      If all you want to do is play with %INC, instead of actually inspecting the symbol table properly, then you might as well use the standard core module, Module::Loaded.
        What version of Perl did Module::Loaded become core in? I'm using 5.8.8 and it is not available there.
        $ perl -MModule::Loaded Can't locate Module/ in @INC (@INC contains: /usr/lib64/perl5 +/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5 +.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_ +64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5 +/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/li +b/perl5/5.8.8 .). BEGIN failed--compilation aborted.
      Kind of. Remember that a file can define more than one package, e.g.
      $ echo 'package A; package B; 1' >; perl -MA -le 'print for keys +%INC'
      If you want to know if a package is defined, rather than whether a correspondingly-named file has been loaded, the OP's approach is correct.
Re: How to tell if a package is loaded
by toolic (Bishop) on May 02, 2011 at 22:56 UTC
    Another way is to inspect %INC
    if (exists $INC{''}) { print "FindBin is loaded\n"; }
Re: How to tell if a package is loaded
by Khen1950fx (Canon) on May 03, 2011 at 03:17 UTC
    Using Module::Load::Conditional to find out everything you never wanted to know about FindBin:
    #!/usr/bin/perl use strict; use warnings; use Module::Load::Conditional qw(check_install requires); my $rv = check_install( module => 'FindBin') or print "FindBin is not installed.\n"; print "FindBin is up to date ", "\n", if $rv->{uptodate}; print "The version number is $rv->{version}\n"; print "It is installed as file $rv->{file}\n\n"; print "The following modules are required:\n"; print join "\n", requires('FindBin'), "\n";
Re: How to tell if a package is loaded
by ambrus (Abbot) on May 03, 2011 at 08:03 UTC

    Just check for the truth value of $FindBin::VERSION.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://902578]
Approved by toolic
[Corion]: I should tell them about the "dim+lock all monitors to show the immediate alert centered" feature of that software so they can announce the next intranet website feature even better ;)
[Eily]: This sounds like the perfect medium to send "don't let anything distract you"
[LanX]: hippo you asked about translating Asterix ?
[Eily]: Maybe the puns in his dialogues were somehow kept through translation, but I can tell he lost his accent
[LanX]: qui?
[Eily]: Astérix
1nickt gives up on finding a Perl YAML parser that supports hash merging. In 2017.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2017-05-24 14:52 GMT
Find Nodes?
    Voting Booth?