Finding A Perl Module's Path[转]
来源:互联网 发布:我的世界符文矩阵 编辑:程序博客网 时间:2024/06/03 20:59
Find a Perl Module's Path
Posted in 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?
- Finding A Perl Module's Path[转]
- A Bug's Life -Poj
- A Beginner's Python Tutorial
- 内部比赛 A Alfredo\'s Pizza Res…
- HDOJ 4337 King Arthur's Knigh…
- Lesson 1 Finding …
- HDU 1162 Eddy's picture
- uva 10010 - Where's Waldorf?
- poj 2488 A Knight's Journey
-  
- Le le's picture when aged 5 mont…
- HDOJ 1161 Eddy's mistakes
- HDOJ 1210 Eddy's 洗牌问题
- HDOJ 1162 Eddy's picture
- HDOJ 2101 A + B Problem Too
- Cannot create a s…
- can't call a non static method f…
-
- Count number of times matched
- A Beginner's Python Tutorial
- PERL中删除文件夹及内容的函数
- e = e || window.event用法细节讨论
- Installing other Perl modules wi…
- Finding A Perl Module's Path[转]
- perl处理properties文件[转]
- Perl script 导出 DataStage jobs
- 快速掌握查看数据库表死锁的实用方…
- CygWin 常用命令[转]
- What is a Perforce "shelved" fil…
- flex&java通信错误之一:Server.re…
- 转载——常见的RemoteObject错误
- Android编译错误处理[转]