Finding a module's $VERSION programmatically

B

Brian Greenfield

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?
 
A

Anno Siegel

Brian Greenfield said:
I'm trying to find the version number of the CPAN modules I've
installed so I can keep my modules up to date.

You get this functionality from CPANPLUS (not sure if the predecessor
CPAN already had it). It compares versions of installed modules with
current versions on CPAN.
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?

There is no formal requirement for a CPAN module to have a version.
Modules that are part of a larger distribution often don't have a
version of their own. Class::DBI::Loader::GraphViz could be one
of those.

For an alternative (but not fundamentally different) approach to
version reading look at base.pm.

Anno
 
B

Brian Greenfield

You get this functionality from CPANPLUS (not sure if the predecessor
CPAN already had it). It compares versions of installed modules with
current versions on CPAN.

Noted.

[Script snipped]

I should have read the error message more carefully. Doh!

ISTM that my brain is flawed:(

After a couple of hours away from the problem, the solution became
clear: make sure I have all the module dependencies installed
For an alternative (but not fundamentally different) approach to
version reading look at base.pm.

Will do. Thanks.
 
P

Paul Lalli

Anno Siegel said:
You get this functionality from CPANPLUS (not sure if the predecessor
CPAN already had it). It compares versions of installed modules with
current versions on CPAN.

CPAN.pm does indeed offer this functionality, with the 'r' command from the
CPAN shell. It will give a list of your currently installed modules that
are less up-to-date than the versions currently available on the CPAN.
(Note that - I believe unlike CPANPLUS - there is no functionality to
automatically download and install the newer versions based on this list).

Paul Lalli
 
T

Tassilo v. Parseval

Also sprach Paul Lalli:
CPAN.pm does indeed offer this functionality, with the 'r' command from the
CPAN shell. It will give a list of your currently installed modules that
are less up-to-date than the versions currently available on the CPAN.
(Note that - I believe unlike CPANPLUS - there is no functionality to
automatically download and install the newer versions based on this list).

There is, although no obvious way: You create a bundle with
'autobundle', make sure the newly created file is in @INC and then do a
'install Bundle::Snapshot_<DATE>.pm'

That works best if you have the latest perl installed, otherwise it will
try to grab and compile a new perl.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top