开发者

Finding the path to a Perl module upon loading

开发者 https://www.devze.com 2023-03-03 02:54 出处:网络
I am using a legacy Perl application which loads a library, call it \"blah\". I need to know where does \"blah\" resides in my file system.

I am using a legacy Perl application which loads a library, call it "blah". I need to know where does "blah" resides in my file system.

I am not fam开发者_Go百科iliar at all with Perl, and I wonder what is the equivalent way to print the path to the module, along the lines of the special variable __file__ in Python. In other words, the Perl equivalent of the following Python script:

import blah
print blah.__file__

Many thanks.


use blah;
print $INC{'blah.pm'};

use Blah1::Blah2::blah;
print $INC{'Blah1/Blah2/blah.pm'};


The case is significant, even on Windows. use Blah will create an entry for $INC{'Blah.pm'} and use blah will create an entry for $INC{'blah.pm'}.

C:\>perl -MList::util -e "print join $/, keys %INC"
XSLoader.pm
Carp.pm
warnings/register.pm
Exporter.pm
vars.pm
strict.pm
List/util.pm
warnings.pm


To expand on my comment on mob's answer, try a more loose use of %INC to help you:

#!/usr/bin/perl

use strict;
use warnings;

use blah;

foreach (keys %INC) {
  if (m'blah.pm') {
    print "$_ => $INC{$_}\n"; 
  }
}

The relevant perldoc perlvar on the subject says

%INC

The hash %INC contains entries for each filename included via the do, require, or use operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found. The require operator uses this hash to determine whether a particular file has already been included.

If the file was loaded via a hook (e.g. a subroutine reference, see require for a description of these hooks), this hook is by default inserted into %INC in place of a filename. Note, however, that the hook may have set the %INC entry by itself to provide some more specific info.

If even that doesn't help, you may, as the previous document suggests, read about the require command, to help you understand how it is getting to be loaded in the first place. This should help you back it out, perhaps by iterating through @INC, which are the folders that Perl will search for to find files to be required.


I found the following one-liner which solved my problem:

$ perl -MList::Util -e'print $_ . " => " . $INC{$_} . "\n" for keys %INC'

Where -MList::Util stands for the List::Util module (in my case, I used -MBlah)

0

精彩评论

暂无评论...
验证码 换一张
取 消