tags:

views:

131

answers:

5

Lot's of ways to set your modules $VERSION in perl, some even have advantages. What I don't know is why we do it? I mean the META.yml has a version... which is what cpan uses? so why do we set it in the module? what's the point?

+3  A: 

So if your module is widely used and actively maintained, people can reference the version easily in their code to activate/deactivate their code depending on what version they use. Just a guess.

Andrew
+3  A: 

$VERSION is available in code. For example, a user of your module can write

use YourModule 2.3;

to ensure that a minimum version of YourModule is available. This is important for API changes, bug fixes, etc.

Michael Carman
+10  A: 

So you can say

use Module::Name 4.5.6;

And the code will fail if you don't have at least version 4.5.6 of Module::Name installed.

It is also helpful when you need to know what version is installed, you can just say:

perl -MScalar::Util=99999999999999

This is roughly equivalent to

#!/usr/bin/perl

use Scalar::Util 99999999999999;

It will fail (becuase Scalar::Util is nowhere near version 99999999999999) and tell you the version number. In my case it says:

Scalar::Util version v.Inf required--this is only version 1.22 at
/Users/cowens/apps/perlbrew/perls/perl-5.12.1/lib/5.12.1/Exporter/Heavy.pm
line 120.
BEGIN failed--compilation aborted.
Chas. Owens
I like the one-liner trick. I beats having to type the full module name twice: `perl -MScalar::Util -e "print $Scalar::Util::VERSION"`
toolic
@toolic You can save yourself one character when you do that way by using the `VERSION` method, rather than the `$VERSION` scalar: `perl -MScalar::Util -E "say Scalar::Util->VERSION"`, but yeah, having to repeat yourself sucks.
Chas. Owens
have a look at `pmvers` from the pmtools package if you want to know which version of a package is installed
plusplus
@toolic, @Chas: you should do `-m` rather than `-M`, to avoid importing methods.
Ether
@plusplus Will `pmtools` be installed on every machine I got to? No? Thanks, but I will stick with the simple command that works everywhere.
Chas. Owens
@Ether I guess it saves you from hitting shift, but it doesn't really do anything here.
Chas. Owens
+5  A: 

From perlmodlib: Guidelines for Module Creation:

To be fully compatible with the Exporter and MakeMaker modules you should store your module's version number in a non-my package variable called $VERSION.

To supplement the Answers given by others, here is the link to use MODULE VERSION

toolic
+3  A: 

This might be a little off topic, but since perl 5.12 you can set your modules VERSION simply by doing

package Foo::Bar 1.23;

See perl 5.12 changes.

The version number must adhere to the "strict" format though, see $version::STRICT in version::Internals.

Øyvind Skaar
I've added a link for you. I don't know what the problem was though...
Philip Potter
Thanks, because my reputations is so low the spam prevention kicks in and denies me more than 1 link pr. post.
Øyvind Skaar
@gizzlon yeah I was aware of that ;) it's why I said there are many ways to define $VERSION. I can think of no less than 4. But I won't start using that syntax until 5.14.1 (is available out of testing in Arch ) at the earliest.
xenoterracide