tags:

views:

1345

answers:

7

in Perl, when I do use < module name> < ver>, the system finds the .pm for the library somewhere in the @INC path.

Is there a reliable way to which file was actually loaded?

A: 

From what I found, perl will look in the directories within the path:

perl -le 'print foreach @INC'

Then it will look in the standard library, and then in the current directory.

That gives you a search order.

My source:

Extending the library path

John at CashCommons
+2  A: 

There are a good number of modules in CPAN for finding this information, the most likely candidates look to be Module::Find, Module::Which, and Module::Locate. I'd probably go with Module::Locate:

use strict;
use warnings;
use Module::Locate qw/locate/;

my $to_find = "Some::Module";

print "Perl would use: ", scalar locate($to_find), "\n";

In list context locate returns all found copies of Some::Module in order based on @INC.

Edit: derobert suggests a much better way, I didn't realize %INC already had it... This module based solution would still be useful if you wanted to know before loading the module where it was going to be loaded from.

mikegrb
+23  A: 

Yes, %INC contains the full path a module was loaded from. Example:

$ perl -M'Data::Dump qw(pp)' -e 'pp(\%INC)'
{
  "Data/Dump.pm"         => "/usr/share/perl5/Data/Dump.pm",
  "Exporter.pm"          => "/usr/share/perl/5.10/Exporter.pm",
  "List/Util.pm"         => "/usr/lib/perl/5.10/List/Util.pm",
  "Scalar/Util.pm"       => "/usr/lib/perl/5.10/Scalar/Util.pm",
  "XSLoader.pm"          => "/usr/lib/perl/5.10/XSLoader.pm",
  "overload.pm"          => "/usr/share/perl/5.10/overload.pm",
  "strict.pm"            => "/usr/share/perl/5.10/strict.pm",
  "vars.pm"              => "/usr/share/perl/5.10/vars.pm",
  "warnings.pm"          => "/usr/share/perl/5.10/warnings.pm",
  "warnings/register.pm" => "/usr/share/perl/5.10/warnings/register.pm",
}
derobert
You learn something new everyday.
Brad Gilbert
perl -MData::Dump=pp -e 'pp(\%INC)'
Brad Gilbert
Your version wins in golf :-D
derobert
So would I be out of luck if I get ' "pp" is not exported by the DBI::Profile module ' if I was checking on DBI::Profile?
Marcus
@Marcus: pp is exported by Data::Dump. Not sure what you ran to get that error, but I doubt it was `perl -M'Data::Dump qw(pp)' -e 'pp(\%INC)'`. It should be noted that if you don't have Data::Dump, you can use Data::Dumper instead (or several other similar modules). Or a while loop.
derobert
If you just want to see where DBI::Profile is, I suggest: `perl -MDBI::Profile -E 'say $INC{"DBI/Profile.pm"}'` which outputs /usr/lib/perl5/DBI/Profile.pm here. Note that -E and say is a 5.10 feature; for older perl you'll need to use -e, print, and put a \n at the end.
derobert
A: 

Module::Mapper provides a way to walk the @INC tree to find modules:

perl -MModule::Mapper -MData::Dumper \
-e 'print Dumper( find_sources( UseINC => 1, Modules => [ @ARGV ] ) )' \
list-of-modules-to-locate
Hudson
+4  A: 

You want to look in the %INC variable, which records the filename it loaded for the libraries you load with do, require, or use. See perlvar for the details.

brian d foy
+2  A: 

If the module has pod documentation, and if you can guarantee that the perldoc utility in your PATH belongs to the same perl that is running your script, then this command will often give you the actual file being found:

perldoc -l ModuleName
skiphoppy
A: 

one-liner (with a bash wrapper) to determine if a module is installed - and where it will be loaded from:

#!/bin/bash
perl -M${1} -le "\$mname=\"${1}.pm\";\$mname=~s#::#/#g;print \"$1 INSTALLED AT \$INC{\$mname}\";" 2>/dev/null || echo "${1} NOT INSTALLED"

call with perl's module name syntax:

./find_perl_module  Font::Metrics::Courier
Russell Jungwirth