http://www.perlmonks.org?node_id=902578

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?

UPDATE:

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/Loaded.pm 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' > A.pm; perl -MA -le 'print for keys +%INC' A.pm
      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{'FindBin.pm'}) { 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.