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

Is there a way to know the path to the modules that have been loaded?

I'd like to include this in the debugging output of a script I'm writing.

I've had situations where I've run a script and it craps out because of an invalid module version, then I install the correct version and it still craps out, making me dizzy until I realize I've got two versions installed. I'd like to make that situation easier on my users by giving them the option to print out exactly which file they're running.

Replies are listed 'Best First'.
Re: Which module did I load?
by friedo (Prior) on Aug 21, 2006 at 20:56 UTC
    Take a look at the %INC hash, which is documented in perldata perlvar.

    $INC{'Module/Name.pm'} contains the filename that corresponds to Module::Name.

    Update: Oops, the key is Module/Name.pm, not Module::Name.

      UPDATE OOPS! I misread your post. I'm a moron. Please slap me with a herring..

      Hmmm... I really need the full path.

      Module/Name.pm is helpful, but I really need

      /usr/lib/perl5/site_perl/5.8.8/Module/Name.pm so I know it's different from /usr/lib/perl5/site_perl/5.6.1/Module/Name.pm.

      Does that make sence?

        The values of %INC contain the full paths to the modules.

Re: Which module did I load?
by jhourcle (Prior) on Aug 22, 2006 at 00:27 UTC

    I keep the following shell script in my path as 'whichpm' :

    #!/bin/sh echo 'print map { sprintf( "%20s : %s\n", $_, $INC{$_} ) } sort keys % +INC; print "\n'$1' version : $'$1'::VERSION\n\n"' | perl "-M$1"

    Which yields results such as :

    $ whichpm CGI CGI.pm : /System/Library/Perl/5.8.6/CGI.pm CGI/Util.pm : /System/Library/Perl/5.8.6/CGI/Util.pm Carp.pm : /System/Library/Perl/5.8.6/Carp.pm Exporter.pm : /System/Library/Perl/5.8.6/Exporter.pm constant.pm : /System/Library/Perl/5.8.6/constant.pm overload.pm : /System/Library/Perl/5.8.6/overload.pm strict.pm : /System/Library/Perl/5.8.6/strict.pm vars.pm : /System/Library/Perl/5.8.6/vars.pm warnings.pm : /System/Library/Perl/5.8.6/warnings.pm warnings/register.pm : /System/Library/Perl/5.8.6/warnings/register.pm CGI version : 3.05
Re: Which module did I load?
by renodino (Curate) on Aug 21, 2006 at 23:16 UTC
    I believe you'll find a solution in Module::Util... esp. the find_installed($module_name) method