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

Hi fellow monks,

I want to monitor that a list of CPAN modules are installed and correct versioned on a number of computers. (I intend to to run the script using the excellent GPL'ed tool cfengine www.cfengine.org, but that beside the point.)

Several modules, including CPAN.pm, have a non-numeric version number (i.e. "$VERSION = '1.59_54';"). Without knowing how future versions of modules evolve, how would you create a general version comparison that checks the desired modules are installed in (at least) the version you want?

Sample script:
#!/usr/bin/perl use strict; use warnings; use ExtUtils::Installed; use constant TRUE => 0; use constant FALSE => -1; use constant DEBUG => 1; my $params = shift; # cfengine sends params as single string my ($modulename, $requiredversion) = split(' ', $params); die "missing modulename!" unless $modulename; my $instmod = ExtUtils::Installed->new(); foreach my $module ((grep /^$modulename$/, $instmod->modules())) { my $version = $instmod->version($module); if ($version >= $requiredversion){ print STDERR "$modulename installed:$version required:$requiredve +rsion - ok\n" if DEBUG; exit(TRUE); # installed and in correct version } else { print STDERR "$modulename installed:$version required:$requiredve +rsion - failed\n" if DEBUG; exit(FALSE); # installed, but not correct version } } print STDERR "$modulename not installed - failed\n" if DEBUG; exit(FALSE); # false __END__
# ./cpanModVer.pl HTML::Mason 1.1 HTML::Mason installed:1.23 required:1.1 - ok # ./cpanModVer.pl CPAN 1.0 Argument "1.59_54" isn't numeric in numeric ge (>=) at ./cpanModVer.pl + line 36.
Linux 2.4.20-28.7 i686 This is perl, v5.6.1 built for i386-linux (GNU cfengine 2.1.7p1)

Replies are listed 'Best First'.
Re: General version comparison of CPAN modules
by wolv (Pilgrim) on Jul 28, 2004 at 14:24 UTC
    See the version (lowercase) module, which overloads the appropriate comparison operators to handle "1.59_54" style version numbers as objects, among others. Be sure to read the POD carefully.
Re: General version comparison of CPAN modules
by davido (Cardinal) on Jul 28, 2004 at 15:37 UTC

    I have used a great little module called, Sort::Versions a few times. This module has a sub called versioncmp() that takes two version numbers as arguments, and returns -1, 0, or 1, like the 'cmp' operator, depending on which arg it thinks represents the later version number. It seems to belive that "1_5" is greater than "1.5". This may or may not be what you're looking for.

    I strongly encourage you to have a look at the POD for the module... on CPAN under Sort::Versions. There are two versions of the module there, so be sure that you're looking at the later one (ironic isn't it?). The POD discusses the comparison algorithm so that you won't have any surprises.

    For a simple real world example of this module in use, have a look at Check websites for software version updates (ex. Garmin), where I use the versioncmp() function to determine whether the Garmin website has posted updates for the firmware for my GPS, or its PC-side software. It works great.


    Dave

Re: General version comparison of CPAN modules
by Anonymous Monk on Jul 28, 2004 at 15:04 UTC
    Modules with with _ in the version number are development versions.
Re: General version comparison of CPAN modules
by ysth (Canon) on Jul 28, 2004 at 15:23 UTC
    I would just (untested):
    $version =~ s/^([\d.]+_[\d.]+)\z/$1/ee;