简体   繁体   English

加载时查找 Perl 模块的路径

[英]Finding the path to a Perl module upon loading

I am using a legacy Perl application which loads a library, call it "blah".我正在使用加载库的旧版 Perl 应用程序,称之为“blah”。 I need to know where does "blah" resides in my file system.我需要知道“blah”在我的文件系统中的位置。

I am not familiar 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.我对 Perl 完全不熟悉,我想知道按照 Python 中的特殊变量__file__的行,打印模块路径的等效方法是什么。 In other words, the Perl equivalent of the following Python script:换句话说,Perl 等效于以下 Python 脚本:

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.这个案例很重要,即使在 Windows 上也是如此。 use Blah will create an entry for $INC{'Blah.pm'} and use blah will create an entry for $INC{'blah.pm'} . use Blah将为$INC{'Blah.pm'}创建一个条目, use blah将为$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:要扩展我对 mob 答案的评论,请尝试更宽松地使用%INC来帮助您:

#!/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关于该主题的相关perldoc perlvar

%INC %INC

The hash %INC contains entries for each filename included via the do, require, or use operators. hash %INC 包含通过 do、require 或 use 运算符包含的每个文件名的条目。 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. require 运算符使用此 hash 来确定是否已包含特定文件。

If the file was loaded via a hook (eg a subroutine reference, see require for a description of these hooks), this hook is by default inserted into %INC in place of a filename.如果文件是通过挂钩加载的(例如,子程序引用,请参阅这些挂钩的说明),默认情况下,该挂钩会插入到 %INC 中以代替文件名。 Note, however, that the hook may have set the %INC entry by itself to provide some more specific info.但是请注意,该挂钩可能已自行设置 %INC 条目以提供一些更具体的信息。

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.如果即使这样也无济于事,您也可以像之前的文档所建议的那样,阅读有关require命令的信息,以帮助您了解如何首先加载它。 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 require d.这应该可以帮助您取消它,也许通过迭代@INC ,这是 Perl 将搜索的文件夹以查找require的文件。

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 )其中-MList::Util代表List::Util模块(在我的例子中,我使用-MBlah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM