Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Which Library Am I Using?

by hey_david (Sexton)
on May 29, 2003 at 15:36 UTC ( #261573=perlquestion: print w/replies, xml ) Need Help??
hey_david has asked for the wisdom of the Perl Monks concerning the following question:

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?


#!/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: <library name>\n" . " E.g.: CGI/\n" . " or: 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/ # strip off the slash + lib name (e.g. strip '/CGI/') 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);

Replies are listed 'Best First'.
Re: Which Library Am I Using?
by ChemBoy (Priest) on May 29, 2003 at 16:25 UTC

    I have the feeling I might be missing some aspect of your script, but I suppose the easiest way to find that out is to volunteer something that I think is equivalent, and find out if it is. That said, the following one-liner does roughly the same thing that I think you're doing above, rather faster:

    perl -le '$mod = shift; eval "use $mod;1;" or die $@; $mod =~ s/::/\// +g; print $INC{$mod.".pm"}' Some::Module

    (It also doesn't fail on an argument of "FileHandle", which I think yours might.)

    Of course, it also does the same thing as perldoc -l Some::Module, but that's a side-issue. :-)

    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Re: Which Library Am I Using?
by Fletch (Chancellor) on May 29, 2003 at 16:04 UTC

    Presumes a 1-to-1 mapping between .pm and package (i.e. this won't always work, but then again neither will your search).

    pmpath () { perl -le "eval { require ${1} }; if( \$@ =~ /locate (\S+)\ ) { print qq{${1} not installed}; exit 1; }(\$m=qq{${1}.pm})=~s{ +::}{/}g;print qq{${1}: }, \$INC{\$m}" } $ pmpath CGI CGI: /usr/lib/perl5/5.8.0/ $ pmpath POE::Wheel::Run POE::Wheel::Run: /usr/lib/perl5/site_perl/5.8.0/POE/Wheel/
Re: Which Library Am I Using?
by zby (Vicar) on May 30, 2003 at 08:16 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://261573]
Approved by Tanalis
[marto]: cpanm let's you install from git
[Spenser]: thanks

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2018-03-17 13:08 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (224 votes). Check out past polls.