导入/要求/使用 perl 模块

发布于 2024-12-10 18:57:47 字数 99 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您可能熟悉 Java 或类似语言如何通过在类路径中查找 com/example/AppName.java 文件来查找 import com.example.AppName 命名空间。当您要求 Perl use HTML::Template 时,它同样会在 @INC 列出的目录中查找 HTML/Template.pm

不同之处在于一个 Perl 文件可以有多个包。当您说 use HTML::Template 时,您将提取 HTML/Template.pm 中列出的所有包(use 自动翻译:: 插入系统的正确目录分隔符,并添加 .pm)。如果该文件中恰好有 HTML::Template::Extension 包,那么您可以使用该包,而无需显式使用它。它已经加载了完整的文件,这就足够了。

如果您知道与运行脚本的位置相关的确切文件名,那么获取它的最简单方法是:

require 'path/to/file.pl';

You may be familiar with how Java or a similar language finds an import com.example.AppName namespace by looking for the com/example/AppName.java file in the classpath. When you ask Perl for use HTML::Template, it likewise looks for HTML/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 in HTML/Template.pm (use automatically translates the :: into the right directory separator for your system, and adds the .pm). If there happens to be a HTML::Template::Extension package in that file, then you can use that package without having to explicitly use 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:

require 'path/to/file.pl';
小霸王臭丫头 2024-12-17 18:57:47

perl @INC 和 Java classpath 之间的比较是我弄清楚如何使用用作本地 Perl 模块的 Perl 文件的关键。虽然 require 文件在问题场景中更实用,但在阅读@frezik 答案后,我将注册如何通过导入模块、use 关键字来实现相同的目标。

必须检查 @INC 数组才能知道我们可以在哪里放置依赖项,这可以使用单行 Perl::

perl -e 'print $_ . "\n" for (@INC);'

对于不使用系统范围安装的我来说,它使得检查一下,上面命令的输出对我来说是这样的:

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/site_perl/5.38.2/x86_64-linux
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/site_perl/5.38.2
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2/x86_64-linux
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2

Linux 操作系统包管理器的系统范围安装可能指向一些更常见的路径,例如 /usr/lib/perl5/...< /code>

我为我的环境目录选择了一些看起来更通用的目录,即使这是一个简单的测试来了解这些可能性:

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2

在这个路径中,我创建了表示属于 Basic::HelloWorld 包的 Hello 模块的结构文件,导致上述目录中相对路径 Basic/HelloWord 内的 Hello.pm 文件。

Hello.pm 文件:

#!/usr/bin/env perl

package Basic::HelloWorld

print "Hello, World.\n";

这样,我的 Perl 环境的 @INC 数组中的某个目录就有这个模块,因为该文件位于这个绝对路径中

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2/Basic/HelloWord/Hello.pm

:使用此模块现在可以引用此依赖项,如下所示:

文件 app.pl,位于某处,因为它使用了您放置本地模块的 Perl 安装:

#!/usr/bin/env perl

use Basic::HelloWorld::Hello

您必须确保入口点具有将执行的用户的执行权限,在本例中为入口点app.pl 文件。因为模块本身只需要该用户的读取权限。因此,您可以执行 app.pl 文件:

./app.pl

或者

perl app.pl

将产生以下输出:

Hello, World.

由于 app.pl 除了使用 Hello 之外不会做任何事情 模块,Hello.pm 文件,输出是由 Hello 模块中的 print 函数生成的。

The comparison between perl @INC and Java classpath was the key to me figuring out how to make use of a Perl file used as a local Perl module. Although require 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::

perl -e 'print $_ . "\n" for (@INC);'

For me, who doesn't use system-wide installation, it makes sense to inspect, the output of the above command is this for me:

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/site_perl/5.38.2/x86_64-linux
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/site_perl/5.38.2
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2/x86_64-linux
/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2

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:

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2

In this path I created the structure file that represents the Hello module that belongs to a Basic::HelloWorld package, resulting in a Hello.pm file within a relative path Basic/HelloWord within the directory mentioned above.

Hello.pm file:

#!/usr/bin/env perl

package Basic::HelloWorld

print "Hello, World.\n";

This way, some directory in the @INC array of my Perl environment has this module, as the file is in this absolute path:

/home/<my-username>/.asdf/installs/perl/5.38.2/lib/5.38.2/Basic/HelloWord/Hello.pm

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:

#!/usr/bin/env perl

use Basic::HelloWorld::Hello

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 the app.pl file:

./app.pl

or

perl app.pl

The will produces the output below:

Hello, World.

Since the app.pl doesn't make anything beyond make use of Hello module, Hello.pm file, the output is generated due to print function in Hello module.

千笙结 2024-12-17 18:57:47

您需要在每个使用模块函数的 pl 文件中声明一个模块。

You need to declare a module in each pl file that is using a function of the module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文