简体   繁体   English

BEGIN块中的Perl模块版本信息

[英]Perl module version information in the BEGIN block

In some CPAN modules I found the next construction 在一些CPAN模块中,我找到了下一个结构

BEGIN {
    $Package::Name::VERSION = "N.N";
}

What is a rationale behind putting the package version information into the BEGIN block? 将包版本信息放入BEGIN块后的理由是什么?

for example: http://cpansearch.perl.org/src/JSWARTZ/Poet-0.12/lib/Poet/Cache.pm 例如: http//cpansearch.perl.org/src/JSWARTZ/Poet-0.12/lib/Poet/Cache.pm

EDIT - in the most recent module version it is pulled out, but anyway - it was here - so wondering why it is good (or bad) :) 编辑 - 在最新的模块版本中它被拔出,但无论如何 - 它在这里 - 所以想知道它为什么好(或坏):)

It's one of those things that everyone has always done and can't remember why! 这是每个人一直做的事情之一,不记得为什么!

Essentially it is because use statements allow multiple modules to be in compilation at the same time, so although it looks like a module is complete it may have only just started compiling. 本质上是因为use语句允许多个模块同时进行编译,所以尽管看起来模块已经完成,但它可能刚刚开始编译。

Any code being compiled can check the value of $Module::VERSION by using the inherited UNIVERSAL::VERSION subroutine, which is already defined implicitly before a module even starts compiling. 正在编译的任何代码都可以使用继承的UNIVERSAL::VERSION子例程检查$Module::VERSION的值,该子例程在模块开始编译之前已经隐式定义。

Remember that, if Module.pm contains 请记住,如果Module.pm包含

use Another::Module;

Then compilation of Module.pm is put on hold while Another/Module.pm is compiled. 然后编译Module.pm ,同时编译Another/Module.pm

There is nothing to stop Another::Module from doing 没有什么可以阻止Another::Module做的

use Module 1.5;

which will call Module::VERSION(1.5) (inherited from UNIVERSAL::VERSION ) to check that $Module::VERSION is 1.5 or greater. 它将调用Module::VERSION(1.5) (继承自UNIVERSAL::VERSION )来检查$Module::VERSION是否为1.5或更高。

If $Module::VERSION is defined outside a BEGIN block, it won't be set until after all use statements have been completed, and so too late for other modules to do version checking on Module . 如果在BEGIN块之外定义$Module::VERSION ,则在完成所有use语句之后才会设置它,因此其他模块对Module进行版本检查的时间太晚。

I hope this is clear. 我希望这很清楚。 I can't help thinking there must be a simpler explanation, but nothing comes to mind. 我不禁想到必须有一个更简单的解释,但没有任何想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM