3

In some CPAN modules I found the next construction

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

What is a rationale behind putting the package version information into the BEGIN block?

for example: 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) :)

3
  • Care to include specific examples where you've seen this? The short answer is so that other initialization steps can refer to it in error messages without risk of it not being set. Also, it doesn't hurt anything even if it is potentially superfluous. I believe there's another reason related to parsing the module for the version number, but I forget the details of that.
    – Miller
    Apr 3, 2014 at 20:53
  • @Miller Don't understand why do you need specific example for the answer, but ok - added one ;)
    – clt60
    Apr 3, 2014 at 20:59
  • Because different modules might be doing it for different reasons (some might just be copy and paste), therefore it's helpful to talk about a specific case. Also, my brain works quicker when looking at actual code ;) I know there's a historical reason behind this, but I don't recall it yet.
    – Miller
    Apr 3, 2014 at 21:04

1 Answer 1

5

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.

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.

Remember that, if Module.pm contains

use Another::Module;

Then compilation of Module.pm is put on hold while Another/Module.pm is compiled.

There is nothing to stop Another::Module from doing

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.

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.

I hope this is clear. I can't help thinking there must be a simpler explanation, but nothing comes to mind.

3
  • So, it helps when looking for the "use specific module version". Greatly explained - only wondering, why Johnatan pulled out the VERSION from the BEGIN in the most recent modules version... but this is not a question for you :) :) thanx.
    – clt60
    Apr 3, 2014 at 21:59
  • @Miller: It's nothing to do with Exporter: you want to look at use.pm.
    – Borodin
    Apr 3, 2014 at 22:08
  • nod, that's why I removed the part about Exporter from my comment. Think my brain can't handle diving into the specifics of this anymore, but it's a good question/suggested answer.
    – Miller
    Apr 3, 2014 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.