Perl Tool Tip: module_info

| No Comments

When developing and deploying Perl code that relies heavily on CPAN modules (and if yours doesn't, it's either really simple, or you are not using Perl to its full potential), I find myself asking the same set of questions over and over again.

  • Do I have ${ModuleX} installed on this machine?
  • What version of ${ModuleX} is installed?
  • There might be more than one copy of ${ModuleX}. Which one is my code using?
  • ${ModuleX} is not behaving. I want to have a look at its source.

A handy (and free) tool to help answer these questions is available from CPAN in the Module::Info distribution. In addition to the library, this distribution also includes the module_info command line script. Run it to find out the version and location of the module in question. For example:

vince@Vince-Laptop:~$ module_info Module::Info

Name:        Module::Info
Version:     0.310
Directory:   /Library/Perl/5.8.6
File:        /Library/Perl/5.8.6/Module/Info.pm
Core module: no

You can quickly find out what version of the module is installed, where it lives, and even whether it is a part of the Perl core distribution. Sadly, Module::Info itself is not in the Perl core, so you'll have to install it and its dependencies from CPAN.

If you can't install the CPAN module for some reason, I hacked together a little script I call qmod that I could carry around on a thumb drive with my dot-files and such. Being just a quick hack, it's not as functional as module_info, but it gets the job done. Here it is in its entirety (less documentation).

#!/usr/bin/perl
use strict;
my $max = 0;
my @list;
for (@ARGV) {
    $max = length($_) > $max ? length($_) : $max;
    eval  "require $_;" ;
    if ( $@ ) {
        push @list, [$_, 'Not Found', ''];
    } else {
        my ($version,$file,$which);
        ($file = "$_.pm") =~ s{::}{/}g; 
        my $varname = $_ . "::VERSION";
        eval "\$version = \$$varname;";
        push @list, [$_, $version, $INC{$file}];
    }#END if
}#END for
printf "%-${max}s  %6s  %s \n", @{$_} for @list;

If you just need to know the location of a module file, here's a nice tip from brian d foy's Mastering Perl: perldoc -l Module::Info.

blog comments powered by Disqus

About this Entry

This page contains a single entry by Vince V. published on February 18, 2008 9:45 PM.

Inside-out Templates in Perl was the previous entry in this blog.

Best Error Message Ever is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.21-en
Creative Commons License
This blog is licensed under a Creative Commons License.