views:

59

answers:

1

I'm a Moose newbie and I wonder if the common

our $VERSION = "0.001";
$VERSION = eval $VERSION;

should also be used in Moose packages, or Moose has some alternative way for version control. Couldn't find a reference in Moose docs.

+7  A: 

As with all perl packages, it is usually a good idea to have a $VERSION defined in them. This allows other things to properly depend on the version of them with all the features they need, either by declaring a dependency in their Makefile.PL or equivalent, or directly when loading the module using use SomeModule 1.23;.

The eval construct you're showing is a kludge. There is a distinction between regular releases of a module, and development releases. Traditionally that has been indicated by a $VERSION with an underscore in it. That means something like 0.001 would be a normal, stable release, while something like 0.001_01 would be a development release.

The eval is used to get rid of that underscore at runtime, while still preserving it in the version string that the various tools, including PAUSE, the Perl Authors Upload SErver, extract. This is to avoid warnings such as 0.001_01 is not numeric in ....

You'll find that idiom in lots of code. Luckily, there's a good alternative to it. Instead of indicating the development vs. non-development status in the version number of individual modules, you can also do that in the release tarball that you might upload to CPAN by using the -TRIAL flag.

Instead of uploading your distribution as My-Distribution-0.001.tar.gz, you can rename it to My-Distribution-0.001-TRIAL.tar.gz. The CPAN tools will pick that up and treat it as a development release accordingly. Note that -TRIAL is not part of the $VERSION, only of the tarball name. Therefore the eval kludge becomes unnecessary.

Also note that there are alternative ways to declare a package's $VERSION. As of perl 5.12.0, you are able to declare it right with in the package declaration:

package My::Package 0.001;

However, none of this is specific to Moose in any way.

rafl
+1 Thanks for the detailed answer and the update re. perl 5.12.0.
David B
Must I put `use 5.012` before the package name? that's ugly...
David B
No, you don't. `package NAME VERSION;` was a syntax error in earlier perls, so this feature doesn't need to be enabled explicitly. You can still do it tho, and get all the other things `use 5.012` does for you for free, like, for example, enabling the `strict` pragma.
rafl