We have multiple installations of some perl modules on our server, and sometimes we want to know which one of 'em a script is calling. (E.g. Maybe we want to know how its user-serviceable parts have been configured.)
So, I wrote a script to figure it out (below), because, well, it seemed like fun at the time. But what I want to know is, is there a one-liner (or there-abouts) to do this?
Thanks!
#!/usr/bin/perl -w
# Give it a Perl library name.
# It tells you the location of that library that
# /usr/bin/perl would use.
# Useful when you've got multiples of the same library
# floating around.
#
# Accomplished by intersecting your @INC array with the
# locate <library> command (so its accuracy depends
# on an up-to-date locate database (see updatedb)).
use strict;
@ARGV == 1 or
die "Usage: whichlib.pl <library name>\n" .
" E.g.: whichlib.pl CGI/Carp.pm\n" .
" or: whichlib.pl CGI::Carp\n";
chomp(my $lib = $ARGV[0]);
if ($lib =~ m/::/) {
$lib =~ s/::/\//g;
$lib = $lib . '.pm';
}
chomp(my @lib_locations = `locate $lib`);
foreach my $inc_path (@INC) { #ex: /usr/share/perl/5.6.1
foreach my $lib_loc (@lib_locations) { #ex: /usr/share/perl/5.6.1/
+CGI/Carp.pm
# strip off the slash + lib name (e.g. strip '/CGI/Carp.pm')
my $lib_loc_path = $lib_loc;
$lib_loc_path =~ s/\/$lib//;
if ($inc_path eq $lib_loc_path) { # found it!
print "$lib_loc\n";
exit(0);
}
}
}
print "No-match-found.\n";
exit(1);