File Coverage

lib/Module/InstalledVersion.pm
Criterion Covered Total %
statement 42 44 95.4
branch 6 8 75.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             package Module::InstalledVersion;
4              
5 1     1   5 use strict;
  1         1  
  1         41  
6 1     1   4 use Carp ();
  1         2  
  1         11  
7 1     1   4 use File::Spec ();
  1         2  
  1         21  
8              
9 1     1   4 use vars '$VERSION';
  1         1  
  1         327  
10             $VERSION = "0.05";
11              
12             =pod
13            
14             =head1 NAME
15            
16             Module::InstalledVersion - Find out what version of a module is installed
17            
18             =head1 SYNOPSIS
19            
20             use Module::InstalledVersion;
21             my $m = new Module::InstalledVersion 'Foo::Bar';
22             print "Version is $m->{version}\n";
23             print "Directory is $m->{dir}\n";
24            
25             =head1 DESCRIPTION
26            
27             This module finds out what version of another module is installed,
28             without running that module. It uses the same regexp technique used by
29             L<Extutils::MakeMaker> for figuring this out.
30            
31             Note that it won't work if the module you're looking at doesn't set
32             $VERSION properly. This is true of far too many CPAN modules.
33            
34             =cut
35              
36             sub new {
37 1     1 0 944     shift;
  1     3   2  
  1         6  
  1         1071  
  1         3  
  1         1  
  1         14  
  3         9  
38 3         11     my ($module_name) = @_;
39 1         798     my $self = {};
  3         10  
40 3 50       2207     $module_name = File::Spec->catfile(split(/::/, $module_name));
  3         73  
41 3         443799  
42 3         49     DIR: foreach my $dir (@INC) {
  3         17  
43 3         5066         my $filename = File::Spec->catfile($dir, "$module_name.pm");
  3         40  
  29         224  
44 29 100       729         if (-e $filename ) {
45 0         0             $self->{dir} = $dir;
  3         17  
46 3 50       189             if (open IN, "$filename") {
47 3         65                 while (<IN>) {
48             # the following regexp comes from the Extutils::MakeMaker
49             # documentation.
50 79 100       205                     if (/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/) {
51 3         10                         local $VERSION;
52 3         269                         my $res = eval $_;
53 3   66     29                         $self->{version} = $VERSION || $res;
54 3         12                         last DIR;
55                                 }
56                             }
57                         } else {
58 0         0                 Carp::carp "Can't open $filename: $!";
59                         }
60                     }
61                 }
62 3         9     bless $self;
63 3         11     return $self;
64             }
65              
66             =head1 COPYRIGHT
67            
68             Copyright (c) 2001 Kirrily Robert.
69             This program is free software; you may redistribute it
70             and/or modify it under the same terms as Perl itself.
71            
72             =head1 SEE ALSO
73            
74             L<Extutils::MakeMaker>
75            
76             =head1 AUTHOR
77            
78             Kirrily "Skud" Robert <skud@cpan.org>
79            
80             =cut
81