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?
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?
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:
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.
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",
}
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
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.
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
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