File Coverage

blib/lib/App/module/version.pm
Criterion Covered Total %
statement 27 109 24.7
branch 0 36 0.0
condition 0 9 0.0
subroutine 9 18 50.0
pod 0 3 0.0
total 36 175 20.5


line stmt bran cond sub pod time code
1             package App::module::version;
2              
3 1     1   24694 use 5.008009;
  1         4  
  1         94  
4 1     1   7 use strict;
  1         1  
  1         30  
5 1     1   5 use warnings;
  1         17  
  1         38  
6              
7 1     1   1247 use Getopt::Long 2.13 qw(GetOptionsFromArray);
  1         18816  
  1         34  
8 1     1   2481 use Pod::Usage qw(pod2usage);
  1         93157  
  1         98  
9 1     1   843 use English qw( -no_match_vars );
  1         4239  
  1         6  
10 1     1   447 use Config;
  1         2  
  1         31  
11 1     1   929 use File::Spec::Functions qw(splitpath catfile);
  1         732  
  1         67  
12 1     1   5 use Carp qw(carp);
  1         2  
  1         1259  
13              
14             # following recommendation from http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
15             our $VERSION = "1.004";
16             $VERSION = eval $VERSION;
17              
18             sub new {
19 0     0 0   my $class = shift;
20 0           return bless {
21             prompt => 0,
22             prompted => [],
23             list => [],
24             }, $class;
25             }
26              
27             sub parse_options {
28 0     0 0   my $self = shift;
29 0           my @a = @_;
30              
31             GetOptionsFromArray(\@a,
32 0     0     'help|?' => sub { pod2usage(-exitstatus => 0, -verbose => 0); },
33 0     0     'man' => sub { pod2usage(-exitstatus => 0, -verbose => 2); },
34 0     0     'usage' => sub { _usage(); },
35 0     0     'version' => sub { _version(); exit(1); },
  0            
36 0 0         'prompt' => \$self->{prompt},
37             ) or pod2usage(-verbose => 2);
38              
39 0 0         if (0 == scalar @a) {
40 0           $self->{prompt} = 1;
41             }
42             else {
43 0           @{$self->{list}} = @a;
  0            
44             }
45             }
46              
47              
48             sub do_job {
49 0     0 0   my $self = shift;
50              
51 0 0         if ($self->{prompt}) {
52 0           _version();
53 0           print "\nPlease type in a space-separated list of modules you want to find\nthe installed versions for below.\n> ";
54 0           my $cmd = ;
55 0           @{$self->{prompted}} = split m{\s+}, $cmd;
  0            
56             }
57              
58 0           print "\n";
59 0           my $version_info;
60 0           MODULE:
61 0           for my $module (@{$self->{list}}, @{$self->{prompted}}) {
  0            
62 0 0         if ('perl' eq lc($module)) {
63 0           print "The version of perl is $PERL_VERSION on $OSNAME ($Config{archname})\n";
64 0           next MODULE;
65             }
66              
67 0 0         if ($module =~ m/\Astrawberry (?:perl)?\z/imsx) {
68 0 0 0       if (('MSWin32' ne $OSNAME) or ($Config{libperl} !~ m{\.a\z}msx)) {
69 0           print "This is not Strawberry Perl.\n";
70             }
71            
72 0 0 0       if (($Config{libperl} =~ m{\.a\z}msx) and ($Config{myuname} !~ m/\AWin32 [ ] strawberryperl/msx )) {
73 0           print "This is not a new enough version of Strawberry Perl to easily tell what version it is.\n";
74 0           next MODULE;
75             }
76            
77 0           my ($strawberryversion, $bits) = (q{}, q{});
78 0 0         if ($Config{myuname} =~
79             m{\AWin32 [ ] strawberryperl [ ] # Starting code.
80             (\S+) # Version
81             .* [ ] # The date Strawberry Perl was built.
82             (\S+)\z # The version
83             }msx) {
84 0           ($strawberryversion, $bits) = ($1, $2);
85 0 0         $bits = ('i386' eq $bits) ? 32 : 64;
86             }
87 0           print "The version of Strawberry Perl is $strawberryversion ($bits-bit), using gcc $Config{gccversion}\n";
88 0           next MODULE;
89             }
90              
91 0 0         if ('activeperl' eq lc($module)) {
92 0           my $buildnumber = eval { return Win32::BuildNumber() };
  0            
93 0 0         if ($EVAL_ERROR) {
94 0           print "This is not ActivePerl (at least, not on Windows.)\n";
95 0           next MODULE;
96             }
97 0           print "The version of ActivePerl is $PERL_VERSION build number $buildnumber\n";
98 0           next MODULE;
99             }
100              
101 0           my $version_info = {};
102 0           my $module_file = catfile(split(/::/, $module));
103              
104 0           DIRECTORY: foreach my $dir (@INC) {
105 0           my $filename = catfile($dir, "$module_file.pm");
106 0 0         if (-e $filename ) {
107 0           $version_info->{dir} = $dir;
108 0 0         if (open IN, "$filename") {
109 0           while () {
110             # the following regexp comes from the Extutils::MakeMaker
111             # documentation.
112 0 0         if (/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/) {
113 0           local $VERSION;
114 0           my $res = eval $_;
115 0   0       $version_info->{version} = $VERSION || $res;
116 0           last DIRECTORY;
117             }
118             }
119             } else {
120 0           carp "Can't open $filename: $!";
121             }
122             }
123             }
124            
125 0 0         if (exists $version_info->{dir}) {
126 0 0         if (exists $version_info->{version}) {
127 0           print "The version of $module in " . $version_info->{dir} . ' is ' . $version_info->{version} . "\n";
128             } else {
129 0           print "$module is installed in " . $version_info->{dir} . ", but does not have a detectable version.\n";
130             }
131             } else {
132 0           print "$module could not be found.\n";
133             }
134             }
135              
136 0           print "\n";
137              
138 0 0         if ($self->{prompt}) {
139 0           require Term::ReadKey;
140 0           my $char = undef;
141 0           print "Press any key to exit.\n";
142 0           $char = Term::ReadKey::ReadKey(-1) until $char;
143             }
144              
145             }
146              
147             sub _version {
148              
149 0     0     my (undef, undef, $script) = splitpath( $PROGRAM_NAME );
150              
151 0           print <<"EOF";
152             This is $script, version $VERSION, which checks the
153             installed version of the modules named on the command line.
154              
155             Copyright 2010 Curtis Jewell.
156              
157             This script may be copied only under the terms of either the Artistic License
158             or the GNU General Public License, which may be found in the Perl 5
159             distribution or the distribution containing this script.
160             EOF
161              
162 0           return;
163             }
164              
165             sub _usage {
166 0     0     my $error = shift;
167              
168 0 0         print "Error: $error\n\n" if (defined $error);
169 0           my (undef, undef, $script) = splitpath( $PROGRAM_NAME );
170              
171 0           print <<"EOF";
172             This is $script, version $VERSION, which checks the
173             installed version of the modules named on the command line.
174              
175             Usage: $script [ --help ] [ --usage ] [ --man ] [ --version ] [ -? ]
176             [--prompt] Module::To::Check ...
177              
178             For more assistance, run $script --help.
179             EOF
180              
181 0           exit(1);
182             }
183              
184             1;
185              
186             __END__