Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Does anyone know how to inherit version form a module without doing...

use Some::Module; our $VERSION = $Some::Module::VERSION || -1;

I want all modules of a bundle to all reflect the same version.

Sounds ugly I know but I can have CVS incriment the toplevel importer modules version, and for all other modules to reflect that they are the same animal.

Of course I could have a module just for containing $VERSION like:-

package Bundle::VERSION; our ($VERSION) = ('$Revision: 1.1 $' =~ /Revision:\s+(\S+)/); # for CV +S sub import{ (shift || '') eq __PACKAGE__ or return 1; no strict 'refs'; ${caller().'::VERSION'} = $VERSION; 1; } package Bundle::Whatever; use Bundle::VERSION; our $VERSION;

But I thought there must be a better way??

Replies are listed 'Best First'.
Re: Inheritting Version
by sgifford (Prior) on Oct 27, 2005 at 03:12 UTC
    I don't think the way you have is too bad. What don't you like about it? It's only a few words in every module, and should never have to change. You're going to have to put something in there to tell it to get the version from elsewhere, and what you've got is probably as good as anything.

    Also, some of the cleverer tricks will confuse some of the programs that extract version numbers from your script (most importantly ExtUtils::MakeMaker). They look for a line assigning to $VERSION and execute that one line. In fact, to avoid confusing them, you probably want to put your use and your assignment on one line; something like:

    use Bundle::VERSION; $VERSION=Bundle::VERSION::$VERSION;

    Thinking about it a bit, this is a little shorter and seems to work:

    our $VERSION=do 't24.version';
    Then just put t24.version somewhere in your %INC path, with something like:
    '1.6.80';
Re: Inheritting Version
by Rhandom (Curate) on Oct 27, 2005 at 02:32 UTC
    While I think this will do it for you I don't think it is much more clear than doing what you have already done.
    package A; use base qw(Exporter); our $VERSION = '0.0.1' our @EXPORT_OK = qw($VERSION); package B; use A qw($VERSION);

    Of course you could also have done: our @EXPORT = qw($VERSION);

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: Inheritting Version
by ioannis (Abbot) on Oct 27, 2005 at 02:48 UTC
    In the following code, it is the version number is defined by the constant pragma, and all package variables of the form our $package::VERSION refer to the constant function which is inherited by default. In this way, all normal queries for version information get the same number from the same source. Is this what you wanted?
    package Person; use constant VERSION => '5.0'; our $VERSION=VERSION; package American; use base 'Person'; our $VERSION=__PACKAGE__->VERSION; package main; use American; print 'version=', $Person::VERSION; print 'version=', $American::VERSION;