加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:28:43 所属栏目:大数据 来源:网络整理
导读:.pm(Perl模块)和.pl(Perl脚本)文件之间的区别是什么? 还请告诉我为什么我们从文件返回1。如果返回2或其他任何东西,它不会生成任何错误,那么为什么我们从Perl模块返回1? 解决方法 在核心,你使用的文件扩展名没有区别perl如何解释这些文件。 但是,将模块
.pm(Perl模块)和.pl(Perl脚本)文件之间的区别是什么?

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

解决方法

在核心,你使用的文件扩展名没有区别perl如何解释这些文件。

但是,将模块放在.pm文件中遵循包名称后的某个目录结构提供了方便。所以,如果你有一个模块Example :: Plot :: FourD,你把它放在一个目录中的例子/ Plot / FourD.pm在你的@INC的路径,然后userequire将做正确的事情,当给定包名称为在使用中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.

所有的用途是从所提供的包名中找出文件名,在BEGIN块中要求它,并在包上调用import。没有什么阻止你不使用,但手动采取这些步骤。

例如,下面我将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 "somethingn" }

"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结束,任何真正的值都会做。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读