blib/lib/V.pm

filter: Statementstmt 100% 95 / 95 Branchbran 61.9% 26 / 42 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 81.6% 142 / 174 SCAR 34.4
CC
28
Cov
84%
version
12.5
all_installed
6.2
report_pkg
4.2
CRAP 31.2
↑ Summary
1 package V;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use vars qw( $VERSION $NO_EXIT );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 $VERSION = '0.16';
6  
7 5 ▶$NO_EXIT ||= 0; # prevent import() from exit()ing and fall of the edge
Condition50%1/2
$V::NO_EXIT ||= 0
l!l
05
MC/DC0%0/1
$V::NO_EXIT ||= 0
$V::NO_EXIT
Truth table50%1/2
A
A= $V::NO_EXIT
Aresult
11
00
8  
9 =head1 NAME
10  
11 V - Print version of the specified module(s).
12  
13 =head1 SYNOPSIS
14  
15 $ perl -MV=V
16  
17 or if you want more than one
18  
19 $ perl -MV=CPAN,V
20  
21 Can now also be used as a light-weight module for getting versions of
22 modules without loading them:
23  
24 require V;
25 printf "%s has version '%s'\n", "V", V::get_version( "V" );
26  
27 If you want all available files/versions from C<@INC>:
28  
29 require V;
30 my @all_V = V::Module::Info->all_installed("V");
31 printf "%s:\n", $all_V[0]->name;
32 printf "\t%-50s - %s\n", $_->file, $_->version
33 for @all_V;
34  
35 Each element in that array isa C<V::Module::Info> object with 3 attributes and a method:
36  
37 =over
38  
39 =item I<attribute> B<name>
40  
41 The package name.
42  
43 =item I<attribute> B<file>
44  
45 Full filename with directory.
46  
47 =item I<attribute> B<dir>
48  
49 The base directory (from C<@INC>) where the package-file was found.
50  
51 =item I<method> B<version>
52  
53 This method will look through the file to see if it can find a version
54 assignment in the file and uses that determine the version. As of version
55 0.13_01, all versions found are passed through the L<version> module.
56  
57 =back
58  
59 =head1 DESCRIPTION
60  
61 This module uses stolen code from L<Module::Info> to find the location
62 and version of the specified module(s). It prints them and exit()s.
63  
64 It defines C<import()> and is based on an idea from Michael Schwern
65 on the perl5-porters list. See the discussion:
66  
67 https://www.nntp.perl.org/group/perl.perl5.porters/2002/01/msg51007.html
68  
69 =head2 V::get_version($pkg)
70  
71 Returns the version of the first available file for this package as found by
72 following C<@INC>.
73  
74 =head3 Arguments
75  
76 =over
77  
78 =item 1. $pkg
79  
80 The name of the package for which one wants to know the version.
81  
82 =back
83  
84 =head3 Response
85  
86 This C<V::get_version()> returns the version of the file that was first found
87 for this package by following C<@INC> or C<undef> if no file was found.
88  
89 =begin implementation
90  
91 =head2 report_pkg
92  
93 This sub prints the results for a package.
94  
95 =head3 Arguments
96  
97 =over
98  
99 =item 1. $pkg
100  
101 The name of the package that was probed for versions
102  
103 =item 2. @versions
104  
105 An array of Module-objects with full path and version.
106  
107 =back
108  
109 =end implementation
110  
111 =head1 AUTHOR
112  
113 Abe Timmerman C<< <abeltje@cpan.org> >>.
114  
115 =head1 COPYRIGHT & LICENSE
116  
117 Copyright 2002-2006 Abe Timmerman, All Rights Reserved.
118  
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.
121  
122 This program is distributed in the hope that it will be useful,
123 but WITHOUT ANY WARRANTY; without even the implied warranty of
124 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
125  
126 =cut
127  
128 sub report_pkg($@) {
129 1 ▶ my $pkg = shift;
Subroutine report_pkg: called CC=4 SCAR=14.3
Pod documented
130  
131 1 print "$pkg\n";
132 1 ▶ @_ or print "\tNot found\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ or print "\tNot found\n" 0 1 1
133 1 for my $module ( @_ ) {
134 2 ▶ printf "\t%s: %s\n", $module->file, $module->version || '?';
Condition50%1/2
$module->version || '?'
l!l
20
MC/DC0%0/1
$module->version || '?'
$module->version
Truth table50%1/2
A
A= $module->version
Aresult
11
00
135 }
136 }
137  
138 sub import {
139 1 ▶ shift;
Subroutine import: called CC=4 SCAR=14.2
140 1 ▶ @_ or push @_, 'V';
Branch: 1 branch
BranchTrueFalseTotal
@_ or push @_, "V" 0 1 1
141  
142 1 for my $pkg ( @_ ) {
143 1 my @modules = V::Module::Info->all_installed( $pkg );
144 1 report_pkg $pkg, @modules;
145 }
146 1 ▶ exit() unless $NO_EXIT;
Branch: 1 branch
BranchTrueFalseTotal
unless $V::NO_EXIT 0 1 1
147 }
148  
149 sub get_version {
150 5 ▶ my( $pkg ) = @_;
Subroutine get_version: called CC=2 SCAR=7.1
Pod documented
151 5 my( $first ) = V::Module::Info->all_installed( $pkg );
152 5 ▶ return $first ? $first->version : undef;
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 5 0 5
153 }
154  
155 5 ▶caller or V->import( @ARGV );
Branch: 1 branch
BranchTrueFalseTotal
caller or "V"->import(@ARGV) 0 5 5
156  
157 5 1;
158  
159 # Okay I did the AUTOLOAD() bit, but this is a Copy 'n Paste job.
160 # Thank you Michael Schwern for Module::Info! This one is mostly that!
161  
162 package V::Module::Info;
163  
164 5 require File::Spec;
165  
166 sub new_from_file {
167 8 ▶ my($proto, $file) = @_;
Subroutine new_from_file: called CC=3 SCAR=11.3
168 8 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
169  
170 8 ▶ return unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 8 8
171  
172 8 my $self = {};
173 8 $self->{file} = File::Spec->rel2abs($file);
174 8 $self->{dir} = '';
175 8 $self->{name} = '';
176  
177 8 return bless $self, $class;
178 }
179  
180 sub all_installed {
181 6 ▶ my($proto, $name, @inc) = @_;
Subroutine all_installed: called CC=6 SCAR=18.2
182 6 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
183  
184 6 ▶ @inc = @INC unless @inc;
Branch: 1 branch
BranchTrueFalseTotal
unless @inc 6 0 6
185 6 my $file = File::Spec->catfile(split m/::/, $name) . '.pm';
186  
187 6 my @modules = ();
188 6 foreach my $dir (@inc) {
189 # Skip the new code ref in @INC feature.
190 48 ▶ next if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 0 48 48
191  
192 48 my $filename = File::Spec->catfile($dir, $file);
193 48 ▶ if( -r $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) 8 40 48
194 8 my $module = $class->new_from_file($filename);
195 8 $module->{dir} = File::Spec->rel2abs($dir);
196 8 $module->{name} = $name;
197 8 push @modules, $module;
198 }
199 }
200  
201 6 return @modules;
202 }
203  
204 # Thieved from ExtUtils::MM_Unix 1.12603
205 sub version {
206 7 ▶ my($self) = shift;
Subroutine version: called CC=12 SCAR=25.3
207  
208 7 my $parsefile = $self->file;
209  
210 7 ▶ open(my $mod, '<', $parsefile) or die "open($parsefile): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $mod, "<", $parsefile or die "open($parsefile): $!" 0 7 7
211  
212 7 my $inpod = 0;
213 7 my $result;
214 7 local $_;
215 7 ▶ while (<$mod>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $mod)) 26 0 26
216 26 ▶ $inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
Branch: 2 branches
BranchTrueFalseTotal
/^=(?!cut)/ ? : 0 26 26
/^=cut/ ? : 0 26 26
217 26 ▶ next if $inpod || /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $inpod or /^\s*#/ 0 26 26
Condition33%1/3
$inpod or /^\s*#/
l!l&&r!l&&!r
0026
MC/DC0%0/2
$inpod or /^\s*#/
$inpod /^\s*#/
Truth table33%1/3
A or B
A= $inpodB= /^\s*#/
ABresult
1X1
011
000
218  
219 26 chomp;
220 26 my $eval;
221 26 ▶ if (m/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/) { } 5 21 26
elsif (/^\s* package \s+ [^\s]+ \s+ ([^;\{]+) [;\{]/x) { } 2 19 21
222 5 { local($1, $2); ($_ = $_) = m/(.*)/; } # untaint
223 5 $eval = qq{
224 package V::Module::Info::_version;
225 no strict;
226  
227 local $1$2;
228 \$$2=undef; do {
229 $_
230 }; \$$2
231 };
232 }
233 # perl 5.12.0+
234 elsif (m/^\s* package \s+ [^\s]+ \s+ ([^;\{]+) [;\{]/x) {
235 2 $eval = qq{
236 package V::Module::Info::_version $1;
237 V::Module::Info::_version->VERSION;;
238 };
239 }
240 26 ▶ if (defined($eval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eval) 7 19 26
241 7 local $^W = 0;
242 7 ▶ $result = eval($eval);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
243 7 ▶ warn "Could not eval '$eval' in $parsefile: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
244 7 ▶ $result = "undef" unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 7 7
245  
246 # use the version modulue to deal with v-strings
247 7 require version;
248 7 $result = version->parse($result);
249 7 last;
250 }
251 }
252 7 close($mod);
253 7 return $result;
254 }
255  
256 sub accessor {
257 9 ▶ my $self = shift;
Subroutine accessor: called CC=2 SCAR=7.0
258 9 my $field = shift;
259  
260 9 ▶ $self->{ $field } = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
261 9 return $self->{ $field };
262 }
263  
264 sub AUTOLOAD {
265 17 ▶ my( $self ) = @_;
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
266  
267 5 ▶ use vars qw( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
268 17 my( $method ) = $AUTOLOAD =~ m|.+::(.+)$|;
269  
270 17 ▶ if ( exists $self->{ $method } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}) 9 8 17
271 9 splice @_, 1, 0, $method;
272 9 goto &accessor;
273 }
274 }
↑ Summary