在 Perl 中,.pm(Perl 模块)和 .pl(Perl 脚本)文件有什么区别?

发布于 2024-09-12 22:59:38 字数 168 浏览 3 评论 0 原文

.pm(Perl 模块)和 .pl(Perl 脚本)文件有什么区别?

另请告诉我为什么我们从文件返回 1 。如果返回 2 或其他任何值,它不会生成任何错误,那么为什么我们从 Perl 模块返回 1 呢?

What is the Difference between .pm (Perl module) and .pl (Perl script) file?

Please also tell me why we return 1 from file. If return 2 or anything else, it's not generating any error, so why do we return 1 from Perl module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

淡写薰衣草的香 2024-09-19 22:59:38

从本质上讲,您使用的文件扩展名对于 perl 如何解释这些文件没有任何影响。

但是,将模块放入遵循包名称后面的特定目录结构的 .pm 文件中会带来便利。因此,如果您有一个模块 Example::Plot::FourD 并将其放入 Example/Plot/FourD.pm 中="http://perldoc.perl.org/perlvar.html#%40INC" rel="noreferrer">@INC,然后 userequire 当给定包名称时,将执行正确的操作,如 use Example::Plot::FourD 中所示。

文件必须返回 true 作为最后一条语句,以指示成功执行任何初始化代码,因此通常以 1; 结束此类文件,除非您确定它会返回 true,否则。但最好只放置 1;,以防您添加更多语句。

如果 EXPR 是一个裸字,则 require 假定为“.pm”扩展名,并在文件名中将“::”替换为“/”,以使轻松加载标准模块。这种形式的模块加载不会有改变您的命名空间的风险。

use 所做的只是从提供的包名称中找出文件名,在 BEGIN 块中 require 并调用 import代码> 包装上。没有什么可以阻止您不使用 use 而是手动执行这些步骤。

例如,下面我将 Example::Plot::FourD 包放入名为 t.pl 的文件中,并将其加载到文件 s.pl 中的脚本中

C:\Temp> cat t.pl
package Example::Plot::FourD;

use strict; use warnings;

sub new { bless {} => shift }

sub something { print "something\n" }

"Example::Plot::FourD"

C:\Temp> cat s.pl
#!/usr/bin/perl
use strict; use warnings;

BEGIN {
    require 't.pl';
}

my $p = Example::Plot::FourD->new;
$p->something;


C:\Temp> s
something

此示例表明模块文件不必以 1 结尾,任何 true 值都可以。

At the very core, the file extension you use makes no difference as to how perl interprets those files.

However, putting modules in .pm files following a certain directory structure that follows the package name provides a convenience. So, if you have a module Example::Plot::FourD and you put it in a directory Example/Plot/FourD.pm in a path in your @INC, then use and require will do the right thing when given the package name as in use Example::Plot::FourD.

The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1;, in case you add more statements.

If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace.

All use does is to figure out the filename from the package name provided, require it in a BEGIN block and invoke import on the package. There is nothing preventing you from not using use but taking those steps manually.

For example, below I put the Example::Plot::FourD package in a file called t.pl, loaded it in a script in file s.pl.

C:\Temp> cat t.pl
package Example::Plot::FourD;

use strict; use warnings;

sub new { bless {} => shift }

sub something { print "something\n" }

"Example::Plot::FourD"

C:\Temp> cat s.pl
#!/usr/bin/perl
use strict; use warnings;

BEGIN {
    require 't.pl';
}

my $p = Example::Plot::FourD->new;
$p->something;


C:\Temp> s
something

This example shows that module files do not have to end in 1, any true value will do.

烟花易冷人易散 2024-09-19 22:59:38

.pl 是一个脚本。

.pm (Perl 模块) 中,您具有可以从其他 Perl 脚本使用的函数:

Perl 模块是一段独立的 Perl 代码,可供 Perl 程序或其他 Perl 模块使用。它在概念上类似于 C 链接库或 C++ 类。

A .pl is a single script.

In .pm (Perl Module) you have functions that you can use from other Perl scripts:

A Perl module is a self-contained piece of Perl code that can be used by a Perl program or by other Perl modules. It is conceptually similar to a C link library, or a C++ class.

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