CJ7
CJ7

Reputation: 23295

How can I inspect the code of a perl module installed?

If I know that a certain perl module is installed on a system, eg. MyCompany::Database::Utils, how can I inspect the perl code of this module?

Upvotes: 3

Views: 997

Answers (3)

Borodin
Borodin

Reputation: 126762

The built-in hash %INC documented in perldoc perlvar relates each module's .pm source file to its file system location

If you have

use MyCompany::Database::Utils;

then perl will search for a file like MyCompany/Database/Utils.pm relative to any of the directories listed in array @INC and, if it is found, will put its absolute location into the %INC hash

To find where each module has been located, you can simply dump the entire hash using Data::Dump or Data::Dumper. But if you're really only interested in one module then you can examine the relevant hash element. A statement like this

print "$INC{'MyCompany/Database/Utils.pm'}\n";

will show the absolute path where that .pm file was found and loaded

Upvotes: 3

ikegami
ikegami

Reputation: 386601

Perl can find the module, so let Perl tell you where it found it!

perl -e'
   my $p = $ARGV[0];
   $p =~ s{::}{/}g;
   $p .= ".pm";
   require $p;
   print "$INC{$p}\n";
' MyCompany::Database::Utils

If the module contains POD, you can use the following shortcut:

perldoc -l MyCompany::Database::Utils

If that doesn't find the module, it could be that the script that uses MyCompany::Database::Utils manipulates @INC to allow it to find the module. If so, add the following to your script:

END {
   my $p = "MyCompany::Database::Utils";
   $p =~ s{::}{/}g;
   $p .= ".pm";
   print "$INC{$p}\n";
}

Upvotes: 3

kba
kba

Reputation: 1177

Find the source code file with whatever means your OS provides.

If you're looking for a properly installed module, you can use perldoc -l to find the file or perldoc -m to print the file (thanks @ThisSuitIsBlackNot, @mob):

perldoc -l List::Util
perldoc -m List::Util

If your module is MyCompany::Database::Utils, you know it must be in a path MyCompany/Database/Utils.pm below one of the paths in @INC.

You can list the default @INC paths with

perl -MData::Dumper -e 'print Dumper(\@INC);'

If you are on a system with mlocate (such as most Linux/BSD distros), you can also find the file with

locate MyCompany/Database/Utils.pm

If you want to look into a distribution to see the full source (e.g. to find XS code, README, unit tests etc.), you can use cpanminus' --look flag:

cpanm --look DateTime

Upvotes: 7

Related Questions