Module::InstalledVersion - Find out what version of a module is installed


Module-InstalledVersion documentation Contained in the Module-InstalledVersion distribution.

Index


Code Index:

NAME

Top

Module::InstalledVersion - Find out what version of a module is installed

SYNOPSIS

Top

    use Module::InstalledVersion;
    my $m = new Module::InstalledVersion 'Foo::Bar';
    print "Version is $m->{version}\n";
    print "Directory is $m->{dir}\n";

DESCRIPTION

Top

This module finds out what version of another module is installed, without running that module. It uses the same regexp technique used by Extutils::MakeMaker for figuring this out.

Note that it won't work if the module you're looking at doesn't set $VERSION properly. This is true of far too many CPAN modules.

COPYRIGHT

Top

SEE ALSO

Top

Extutils::MakeMaker

AUTHOR

Top

Kirrily "Skud" Robert <skud@cpan.org>


Module-InstalledVersion documentation Contained in the Module-InstalledVersion distribution.
#!/usr/bin/perl -w

package Module::InstalledVersion;

use strict;
use Carp ();
use File::Spec ();

use vars '$VERSION';
$VERSION = "0.05";

sub new {
    shift;
    my ($module_name) = @_;
    my $self = {};
    $module_name = File::Spec->catfile(split(/::/, $module_name));

    DIR: foreach my $dir (@INC) {
        my $filename = File::Spec->catfile($dir, "$module_name.pm");
        if (-e $filename ) {
            $self->{dir} = $dir;
            if (open IN, "$filename") {
                while (<IN>) {
                    # the following regexp comes from the Extutils::MakeMaker
                    # documentation.
                    if (/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/) {
                        local $VERSION;
                        my $res = eval $_;
                        $self->{version} = $VERSION || $res;
                        last DIR;
                    }
                }
            } else {
                Carp::carp "Can't open $filename: $!";
            }
        }
    }
    bless $self;
    return $self;
}