导入/要求/使用 perl 模块
我有 2 个 perl 文件,它们相互依赖。 第一个文件 A 是一个 .pl 文件,其中声明了多个包/模块。 第二个文件 B 将尝试访问文件 A 中声明的众多包之一。如何做到这一点?
I have 2 files perl files which have dependency on one another.
The 1st file, A is a .pl file which multiple package/modules declared inside.
The 2nd file, B will try to access one of the many packages declared in file A. How can that be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能熟悉 Java 或类似语言如何通过在类路径中查找
com/example/AppName.java
文件来查找import com.example.AppName
命名空间。当您要求 Perluse HTML::Template
时,它同样会在@INC
列出的目录中查找HTML/Template.pm
。不同之处在于一个 Perl 文件可以有多个包。当您说
use HTML::Template
时,您将提取HTML/Template.pm
中列出的所有包(use
自动翻译::
插入系统的正确目录分隔符,并添加.pm
)。如果该文件中恰好有HTML::Template::Extension
包,那么您可以使用该包,而无需显式使用
它。它已经加载了完整的文件,这就足够了。如果您知道与运行脚本的位置相关的确切文件名,那么获取它的最简单方法是:
You may be familiar with how Java or a similar language finds an
import com.example.AppName
namespace by looking for thecom/example/AppName.java
file in the classpath. When you ask Perl foruse HTML::Template
, it likewise looks forHTML/Template.pm
in the directories listed in@INC
.The difference is that a Perl file can have multiple packages. When you say
use HTML::Template
, you'll be pulling in all packages listed inHTML/Template.pm
(use
automatically translates the::
into the right directory separator for your system, and adds the.pm
). If there happens to be aHTML::Template::Extension
package in that file, then you can use that package without having to explicitlyuse
it. It's already loaded the complete file, and that's good enough.If you know the exact file name relative where you'll be running the script, the easiest way to grab it is:
perl
@INC
和 Javaclasspath
之间的比较是我弄清楚如何使用用作本地 Perl 模块的 Perl 文件的关键。虽然require
文件在问题场景中更实用,但在阅读@frezik 答案后,我将注册如何通过导入模块、use
关键字来实现相同的目标。必须检查 @INC 数组才能知道我们可以在哪里放置依赖项,这可以使用单行 Perl::
对于不使用系统范围安装的我来说,它使得检查一下,上面命令的输出对我来说是这样的:
Linux 操作系统包管理器的系统范围安装可能指向一些更常见的路径,例如
/usr/lib/perl5/...< /code>
我为我的环境目录选择了一些看起来更通用的目录,即使这是一个简单的测试来了解这些可能性:
在这个路径中,我创建了表示属于
Basic::HelloWorld
包的Hello
模块的结构文件,导致上述目录中相对路径Basic/HelloWord
内的Hello.pm
文件。Hello.pm
文件:这样,我的 Perl 环境的
@INC
数组中的某个目录就有这个模块,因为该文件位于这个绝对路径中:使用此模块现在可以引用此依赖项,如下所示:
文件
app.pl
,位于某处,因为它使用了您放置本地模块的 Perl 安装:您必须确保入口点具有将执行的用户的执行权限,在本例中为入口点
app.pl
文件。因为模块本身只需要该用户的读取权限。因此,您可以执行app.pl
文件:或者
将产生以下输出:
由于
app.pl
除了使用Hello 之外不会做任何事情
模块,Hello.pm
文件,输出是由 Hello 模块中的 print 函数生成的。The comparison between perl
@INC
and Javaclasspath
was the key to me figuring out how to make use of a Perl file used as a local Perl module. Althoughrequire
a file is more practical in the question's scenario, I will register how to achieve the same objective by importing a module,use
keyword, after read @frezik answer.The
@INC
array must be inspected to know where we can place our dependencies, this can be done with the one-liner Perl::For me, who doesn't use system-wide installation, it makes sense to inspect, the output of the above command is this for me:
A system-wide installation by Linux operating system package managers may point to some more common paths, such as
/usr/lib/perl5/...
I choose some for my environment's directory that seem more generic, even if it's a simple test to learn these possibilities:
In this path I created the structure file that represents the
Hello
module that belongs to aBasic::HelloWorld
package, resulting in aHello.pm
file within a relative pathBasic/HelloWord
within the directory mentioned above.Hello.pm
file:This way, some directory in the
@INC
array of my Perl environment has this module, as the file is in this absolute path:The file that should use this module can now reference this dependency,like this:
File
app.pl
, located in somewhere since that make use of the Perl installation that you put your local module:You must ensure that the entry point has execute permission for the user who will execute, in this case the entry point
app.pl
file. Because modules themselves only need read permission for that user. So you can execute theapp.pl
file:or
The will produces the output below:
Since the
app.pl
doesn't make anything beyond make use ofHello
module,Hello.pm
file, the output is generated due to print function in Hello module.您需要在每个使用模块函数的 pl 文件中声明一个模块。
You need to declare a module in each pl file that is using a function of the module.