Back to Top

Tuesday, April 22, 2008

Finding the installed files for modules in Perl

First of all if you want to quickly find out if a Perl module is installed on a system or not, you can do the following:

perl -e"use Foo;"

If the module is installed, this won't print out anything, however it it isn't it should say something like (yes, shame on me, I haven't upgraded to 5.10 yet):

Can't locate Foo.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.

Now lets say you want to find the files associated with a given module (to see how something is implemented for example, or you want to fix a bug locally - although editing the files directly isn't the best solution because a future update might overwrite the fixed version). The simplest solution would be to do something like this:

perl -e"print join(\"\n\", @INC), \"\n\";"

Which should result in something like this:

/etc/perl
/usr/local/lib/perl/5.8.8
/usr/local/share/perl/5.8.8
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.8
/usr/share/perl/5.8
/usr/local/lib/site_perl
.

After which you could inspect each directory listed to see if it contains the module you're looking for. A more advanced solution would be to use ExtUtils::Installed like this:

#!/usr/bin/perl
use strict;
use warnings;
use ExtUtils::Installed;

my ($inst) = ExtUtils::Installed->new();
my @all_files = $inst->files('DBI', 'prog');
print join("\n", @all_files);

In the files call "DBI" is the name of the module (take care that you can use only the top-level module name, so Net is ok, but Net::FTP is not) and the second one if the type of files we're looking for ("prog" means code, meaning that we are not interested in documentation files).

Finally if you want a very powerful solution, take a look at perlwh - a 'which' for Perl Modules. By looking at the code it seems to handle many quircks the basic solution from above can't.

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.