加载模块时 perl 的包含路径中的搜索顺序是什么
假设 @INC
中有三个路径:path1
、path2
和 path3
。在每个路径下,都有一个名为 foo.pm
的模块。如果我现在通过 use foo;
在我的脚本中加载 foo.pm
,那么实际上会加载哪个 foo.pm
?或者换句话说,perl 在 @INC
中搜索路径的顺序是什么?
Let's say there are three paths in @INC
: path1
, path2
and path3
. Under each of these paths, there is a module named foo.pm
. If I now load foo.pm
in my script via use foo;
, which of the foo.pm
s is actually going to be loaded? Or in other words, what is perl's search order for paths in @INC
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
perldoc -v %INC 显示选择的路径:
或者...
require 显示一些隐含搜索顺序的伪代码:
因此,
path1
会首先被搜索。perldoc -v %INC shows which path was chosen:
Or...
require shows some psuedo-code which implies the search order:
Thus,
path1
would be searched first.路径1、路径2、路径3。 Perl 将加载
path1/foo.pm
。你为什么会期望它是其他的?
看看
perlfunc perlvar
,我可以看到它们不没有明确这么说,但他们确实说:我认为那里的提示是列表。期望从头到尾处理列表是很正常的。
您可以将此代码放在
use foo;
语句之前:如果仍然显示
@INC=(/path1, /path2, /path3)
则将此代码在use
语句之后:如果该语句仍然显示
$INC{'foo.pm'}=/path3/foo.pm
,那么我认为您没有指定您的搜索路径以及您可能的搜索路径。您可能认为您在指定为“/path1”的同一目录中有foo.pm
,但很可能您的某些路径混乱了。path1, path2, path3. And perl will load
path1/foo.pm
.Why would you expect it to be any other?
Looking at
perlfunc perlvar
, I can see that they don't explicitly say this, but they do say:I think the hint on there is list. It's unexceptional to expect a list to be processed first-to-last.
You could probably put this code, right before your
use foo;
statement:If that still shows you
@INC=(/path1, /path2, /path3)
then put this after theuse
statement:And if that one still shows
$INC{'foo.pm'}=/path3/foo.pm
, then I think you're not specifying your search paths as well as you might. You might think you havefoo.pm
in the same directory specified as '/path1', but the likelihood is that you've got some path messed up.