Finding A Perl Module's Path[转]

来源:互联网 发布:我的世界符文矩阵 编辑:程序博客网 时间:2024/06/03 20:59

Find a Perl Module's Path

Posted in Programming

Articles for Programming

Perl is an awesome language, made better by the huge assortmentof modules available to extend the language. With modules to serveas everything from database abstraction layers and ORMs to HTTPClient libraries you can often quickly implement projects. Notevery module is created perfectly and you may want to review thecode you're loading into your project, identify bugs or generallypoke around.

While multiple versions of a given module may be installed,finding the version you're using is rather simple if you let Perlitself tell you.

Finding A Perl Module's Path

@INC and %INC provide data related tomodules. @INC is an array of the paths for which Perlwill locate a module's source and load it into the program.%INC is a hash and contains the module name as a key,and the file it was loaded from as the value. It is worth notingthat the module name is written in file-system style (This/That.pm)style as opposed to package-name style (This::That).

Finding the path that a given module is loaded from is as simpleas loading the module and asking for the contains of%INC:

$ perl -MList::Util -e'print $_ . " => " . $INC{$_} . "\n" for keys %INC'

-MList::Util translates into a useList::Util for the Perl one liner. If you wanted to know thepath to LWP instead, you could use -MLWP just aseasily.

Running the above command we get:

$ perl -MList::Util -e'print $_ . " => " . $INC{$_} . "\n" for keys %INC'XSLoader.pm => /usr/lib/perl/5.10/XSLoader.pmwarnings.pm => /usr/share/perl/5.10/warnings.pmwarnings/register.pm => /usr/share/perl/5.10/warnings/register.pmExporter.pm => /usr/share/perl/5.10/Exporter.pmvars.pm => /usr/share/perl/5.10/vars.pmstrict.pm => /usr/share/perl/5.10/strict.pmList/Util.pm => /usr/lib/perl/5.10/List/Util.pm

We now know that the file which is loaded forList::Util is/usr/lib/perl/5.10/List/Util.pm. If the amount ofinformation in %INC is too much for you, you couldinstead ask for only the path to the Perl module which you want toknow:

$ perl -MList::Util -e'print $INC{"List/Util.pm"} . "\n"'/usr/lib/perl/5.10/List/Util.pm

We can of course write a tiny script to make this even moretrivial.

#!/usr/bin/perluse warnings;use strict;for my $module ( @ARGV ) {    my $package = $module;    # From This::That to This/That.pm    s/::/\//g, s/$/.pm/ for $module;    if ( require $module ) {        print $package . " => " . $INC{$module} . "\n";    }}

And then pass the modules we want to know about to it:

$ ./fpm.pl List::Util DBI  List::Util => /usr/lib/perl/5.10/List/Util.pmDBI => /usr/lib/perl5/DBI.pm

Using Perldoc To Find Perl Module's Paths

Knowing how to find the path with Perl itself is immenselyuseful, though, a slightly simpler method is to use perldoc if itis installed.

$ perldoc -lm List::Util/usr/lib/perl/5.10/List/Util.pm

The -l switch instructs perldoc to display the pathfor the file, instead of the POD itself. The -m switchinstructs perldoc to display the entire file for a given module,even if it doesn't have a POD.

The -m switch is important, as if the module doesnot have a POD associated with it, failing to use the-m switch is the difference between getting the pathfor the file and being told there is no documentation found.

While there are a number of methods to find this information,these are among the most simple and least error prone methods toidentify the path to a Perl module you're using. What's yourrecommended method?

0 0
原创粉丝点击