Perl Scripting: Checking CPAN Module Versions


It’s been a while since I’ve talked tech trash. So let’s focus on Perl for a bit. If you’ve worked with Perl, chances are that you’ve worked with CPAN (Comprehensive Perl Archive Network) modules. With CPAN, you can quickly do a lot of things without actually having to write the code yourself (read: cheat). It is kinda like Java APIs and C libraries.

The problem with not writing your own code is that someone else is usually fiddling around with the modules, releasing patches, updating versions, deprecating functions, and basically making your life miserable if you didn’t your homework when assimilating the module into your own code. There’s usually little to worry about new versions as they are backward compatible. However, sometimes, someone, somewhere, will decide to install an older version of a module that just won’t work with your code. That’s occasion enough to implement a CPAN module version check within your code to verify everything’s clean and up to speed.

There are of course various means of retrieving and verifying the module versions. I usually use the ‘require’ command to retrieve the CPAN information (e.g. eval ‘require $cpan_module’; where $cpan_module is a variable containing the module name) and subsequently checking $cpan_module->VERSION.

Note that this is by no means the easiest or most efficient way of doing things. It’s just one of the means that I employ.

Here’s a snippet of perl code that I use in conjunction with shell script to check a list of CPAN module versions:
#!/usr/bin/perl
# check_cpan.pm <module-name> <required-version>
use strict;
my $argcnt=0;
my $badcnt=0;
my $warncnt=0;
my $modcnt=0;
my $errcode=0;
printf ("Checking for required CPAN modules...\n");
printf ("%-20s %-10s %s\n", "MODULE", "VERSION", "STATUS");
printf ("%-20s %-10s %s\n", "------", "-------", "------");
while (@ARGV[$argcnt]) {
  my $cpan_module=@ARGV[$argcnt++];
  my $cpan_version=$ARGV[$argcnt++];
  eval "require $cpan_module";
  $modcnt++;
  if ($@) {
    printf( "%-20s %-10s %s\n",
    $cpan_module, $cpan_version, "FAILED: Not found");
    $badcnt++;
  } elsif (! $cpan_module->VERSION) {
    printf ("%-20s %-10s %s\n", $cpan_module, $cpan_version, "WARNING: Undefined version");
    $warncnt++;
  } elsif ($cpan_module->VERSION < $cpan_version) {
    printf ("%-20s %-10s %s\n", $cpan_module, $cpan_version, "WARNING: Unsupported version - ".$cpan_module->VERSION);
    $warncnt++;
  } else {
    printf (”%-20s %-10s %s\n”, $cpan_module, $cpan_version, “OK: Version - “.$cpan_module->VERSION);
  }
}
my $okcnt=$modcnt - ($badcnt + $warncnt);
printf (”%s modules checked. “, $modcnt);
if ($badcnt > 0) {
  printf (”Failed %s CPAN module tests. “, $badcnt, $modcnt);
  $errcode=1;
}
if ($warncnt > 0) {
  printf (”There were %s warnings. “, $warncnt);
  if ($errcode == 0) {
    $errcode=2;
  }
}
printf (”%s(%d\%) modules OK.\n”, $okcnt, $okcnt / $modcnt * 100);
exit $errcode;

The perl script can be called via shell and takes in any number of <module-name>:<required-version> argument pairs.

For example, to check the versions for module1 and module2 whose minimum versions are 1.2 and 1.3 respectively, run the following from a shell terminal:
perl check_cpan.pm module1 1.2 module2 1.3

Or you can put the modules in a list and call the script as follows:
Module list (cpanls.txt):
module1 1.2
module2 1.3

Calling function:
perl check_cpan.pm `cat cpanls.txt`

The check_cpan.pm perl script returns an error number which can be checked and handled accordingly. 0 = no errors, 1 = there were missing CPAN modules, and 2 = all CPAN modules were found but there were some unknown or unsupported versions.

That’s about it for now. Good luck with your perl.

Related posts:
PIKOM PC Fair: Notebook Models And Prices - Acer
Initial D 4th Stage: Episode 23 and 24 Released!
Mini Upgrade And Server
Air Asia 1,000,000 Free Tickets Update
Max File Size On Disk (NTFS Vs. FAT)


Leave a Comment