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

前端之家收集整理的这篇文章主要介绍了在Perl中,.pm(Perl模块)和.pl(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中的脚本中。

  1. C:\Temp> cat t.pl
  2. package Example::Plot::FourD;
  3.  
  4. use strict; use warnings;
  5.  
  6. sub new { bless {} => shift }
  7.  
  8. sub something { print "something\n" }
  9.  
  10. "Example::Plot::FourD"
  11.  
  12. C:\Temp> cat s.pl
  13. #!/usr/bin/perl
  14. use strict; use warnings;
  15.  
  16. BEGIN {
  17. require 't.pl';
  18. }
  19.  
  20. my $p = Example::Plot::FourD->new;
  21. $p->something;
  22.  
  23.  
  24. C:\Temp> s
  25. something

这个例子表明模块文件不必以1结束,任何真正的值都会做。

猜你在找的Perl相关文章