Finding a module's $VERSION programmatically

From: Brian Greenfield (not-for-replies_at_zombie.org.uk)
Date: 10/30/04


Date: Sat, 30 Oct 2004 12:27:43 GMT

I'm trying to find the version number of the CPAN modules I've
installed so I can keep my modules up to date. The following script
shows the technique I've tried:

    #!/usr/bin/perl
    
    use strict;
    use warnings FATAL=>'all';
    
    for (qw/Text::Table Class::DBI::Loader::GraphViz/)
    {
        print "$_: /", fetch_installed_version($_), "/\n";
    }
    
    sub fetch_installed_version
    {
        my $pkg = shift;
    
        eval "require $pkg";
        my $iver = eval '$'."${pkg}::VERSION";
        die "VERSION is not defined in $pkg\n"
            unless $iver;
        return $iver;
    }

The script gets the version for Text::Table but not for
Class::DBI::Loader::GraphViz:

    Text::Table: /1.107/
    VERSION is not defined in Class::DBI::Loader::GraphViz

This isn't a one off, about a third of the packages produce the same
error.

If I try to get the version from the command line, I get:

    zippy:~/scripts$ perl -MClass::DBI::Loader::GraphViz -we \
                     'print $Class::DBI::Loader::GraphViz::VERSION'
    Base class package "GraphViz::DBI" is empty.
        (Perhaps you need to 'use' the module which defines that
        package first.)
     at /usr/share/perl5/Class/DBI/Loader/GraphViz.pm line 3
    BEGIN failed--compilation aborted at
    /usr/share/perl5/Class/DBI/Loader/GraphViz.pm line 3.
    Compilation failed in require.
    BEGIN failed--compilation aborted.

ISTM that my technique is flawed, so what is the best way to find a
module's $VERSION programmatically?



Relevant Pages