Perl程序如何知道在哪里可以找到包含它使用的Perl模块的文件? [英] How does a Perl program know where to find the file containing Perl module it uses?

查看:214
本文介绍了Perl程序如何知道在哪里可以找到包含它使用的Perl模块的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的Perl程序使用Perl模块,它将如何确定在哪里找到包含模块代码的文件?

If my Perl program uses Perl modules, how will it determine where to find the file containing the module code?

例如,如果程序包含:

use MyModule1;              # Example 1
use This::Here::MyModule2;  # Example 2

它在哪里?

推荐答案

Perl解释器(运行你的perl程序)将使用一个名为 @INC 的特殊数组来搜索包含该模块的文件。

Perl interpreter (which runs your perl program) will use a special array called @INC to search for a file containing the module.

@INC 数组中的每个值都是一个目录名(,但请参阅下面的注释); Perl将使用下面指定的规则在循环中搜索这些目录。 (请参阅这篇SO帖子详细说明了如何确定@INC的内容

Each value in the @INC array is a directory name (but see note below); Perl will search within those directories in a loop using the rules specified below. (Please refer to this SO post for details of how the contents of @INC are determined).

如果在耗尽<$ c $后找不到模块的文件c> @INC ,程序的编译将中止并显示错误。如果在 @INC 中指定的目录之一中找到模块的文件,则搜索结束,而不查看 @INC

If the module's file is not found after exhausting @INC, the program's compilation will be aborted with an error. If the module's file is found in one of the directories specified in @INC, the search is finished without looking at the rest of @INC.

Perl在 @INC 中列出的每个目录中搜索模块文件的方式如下:

The way Perl searches for a module file within each of the directories listed in @INC is as follows:


  • 首先,它将分离模块名称的分层组件(由分隔的单词: :),进入最后一个组件 - 将用于形成文件名 - 和层次结构路径(最后一个 :: )。

  • First, it will separate the module name's hierarchical components (words separated by ::), into the last component - which will be used to form a file name - and a hierarchy path (all the components preceding the last ::).

如果模块名称只有一个组件(没有 :: ,例如 MyModule1 上面),层次结构路径为空,文件名是模块的名称。在这个问题的第二个例子中,最后一个组件是 MyModule2 ,层次结构路径将是 This :: Here

In case the module name has only one component (no ::, e.g. MyModule1 above), the hierarchy path is empty and the filename is the name of the module. In the second example in this question, the last component is MyModule2 and hierarchy path will be This::Here.

预期的文件名将通过附加模块名称的最后一个组件来确定,其中 .pm 延期。例如。我们的示例中 MyModule1.pm MyModule2.pm

The expected file name will be determined by appending the last component of the module name with a .pm extension. E.g. MyModule1.pm and MyModule2.pm in our examples.

注意:在Unix和其他文件/目录命名区分大小写的操作系统上,模块名称显然是区分大小写的。

NOTE: Module names are obviously case sensitive on Unix and other operating systems where file/directory naming is case sensitive.

模块的目录将由:


  1. @INC 获取下一个目录 - 让我们说 / usr / lib / perl 作为示例

  1. Taking the next directory from @INC - let's say /usr/lib/perl as an example

通过以下方式形成该目录的子目录模块名称的层次结构路径(如果有)并将::替换为 / 或操作系统用作目录分隔符的任何字符。在我们的两个示例中,将在 / usr / lib / perl (无子目录)中搜索第一个模块,在 / usr中搜索第二个模块/ lib / perl / This / Here

Forming a sub-directory of that directory by taking the hierarchy path of the module name (if any) and replacing "::" with / or whatever character the operating system uses as directory separator. In our two examples, the first module will be searched for in /usr/lib/perl (no sub-directory) and the second in /usr/lib/perl/This/Here.

注意:以上是略有简化 - @INC may还包含子例程引用和对象引用,它们按照自定义代码指定加载模块,而不是在上面#2逻辑中指定的目录中执行查找。该功能似乎很少使用,本文假设整个 @INC 仅包含目录。

NOTE: the above is a slight simplification - @INC may also contain subroutine references and object references, which load the modules as their custom code specifies instead of performing the lookup in the directory as specified in #2 logic above. That functionality appears to be very seldom used and this article assumes that entire @INC only contains directories.


让我们回顾一个具体的例子,假设您的 @INC 包含两个子-directories:
(/ usr / lib / perl,/ opt / custom / lib)

Let's go over a specific example, assuming that your @INC contains two sub-directories: ("/usr/lib/perl", "/opt/custom/lib").

然后Perl搜索如下:

Then Perl would search as follows:


==========================================================================
| Module                | Try # | File to try               
==========================================================================
| MyModule1             | Try 1 | /usr/lib/perl/MyModule1.pm
| MyModule1             | Try 2 | /opt/custom/lib/MyModule1.pm
==========================================================================
| This::Here::MyModule2 | Try 1 | /usr/lib/perl/This/Here/MyModule2.pm
| This::Here::MyModule2 | Try 2 | /opt/custom/lib/This/Here/MyModule2.pm
==========================================================================

请回想一下,Perl解释器会在其中一个位置找到文件后停止尝试搜索,而不会尝试查看该文件是否也在以后的位置。例如。如果 /usr/lib/perl/This/Here/MyModule2.pm 存在,那么Perl将不会查找,也不关心的存在/opt/custom/lib/This/Here/MyModule2.pm

Please recall that Perl interpreter will STOP trying to search once it finds the file in one of the locations, without trying to see if the file is in later locations as well. E.g. if /usr/lib/perl/This/Here/MyModule2.pm exists, then Perl will not look for, nor care about the existence, of /opt/custom/lib/This/Here/MyModule2.pm.

注意:只要Perl解释器使用<$,就会使用@INC c $ c> require -like机制,用于导入Perl模块。这包括:

NOTE: @INC is used whenever Perl interpreter is using require-like mechanism for importing Perl modules. This includes:


  • require 指令本身

  • 使用MyModule 语句(相当于require + import)

  • 使用base (相当于require +push @ISA)

  • -M 命令行参数

  • require directive itself
  • use MyModule statement (equivalent to require+import)
  • use base (equivalent to require+"push @ISA")
  • -M command line parameter

这篇关于Perl程序如何知道在哪里可以找到包含它使用的Perl模块的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆