blib/lib/V.pm

filter: Statementstmt 95.4% 127 / 133 Branchbran 73.6% 53 / 72 Conditioncond 50.0% 13 / 26 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 80.6% 212 / 263 CC 50 SCAR 27.9
CC
50
Cov
84%
version
30.7
all_installed
9.3
report_pkg
7.3
CRAP 16.3
↑ Summary
1 package V;
2  
3 8 require 5.014000;
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use vars qw( $VERSION $NO_EXIT );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 $VERSION = "0.23";
10  
11 8 ▶$NO_EXIT ||= 0; # prevent import() from exit()ing and fall of the edge
Condition50%1/2
$V::NO_EXIT ||= 0
l!l
08
MC/DC0%0/1
$V::NO_EXIT ||= 0
$V::NO_EXIT
Truth table50%1/2
A
A= $V::NO_EXIT
Aresult
11
00
12  
13 8 our $DEBUG;
14 8 ▶ $DEBUG ||= $ENV{PERL_V_DEBUG} || 0;
Condition40%2/5
$V::DEBUG ||= $ENV{"PERL_V_DEBUG"} || 0
l!l&&r!l&&!r
008
$ENV{'PERL_V_DEBUG'} || 0
l!l
08
MC/DC0%0/2
$V::DEBUG ||= $ENV{"PERL_V_DEBUG"} || 0
$V::DEBUG $ENV{'PERL_V_DEBUG'}
Truth table33%1/3
A ||= B
A= $V::DEBUGB= $ENV{'PERL_V_DEBUG'}
ABresult
1X1
011
000
15  
16 =encoding utf-8
17  
18 =head1 NAME
19  
20 V - Print version of the specified module(s).
21  
22 =head1 SYNOPSIS
23  
24 $ perl -MV=V
25  
26 or if you want more than one
27  
28 $ perl -MV=CPAN,V
29  
30 Can now also be used as a light-weight module for getting versions of
31 modules without loading them:
32  
33 require V;
34 printf "%s has version '%s'\n", "V", V::get_version ("V");
35  
36 Starting with version B<0.17>, V will show all C<package>s or C<class>es in a
37 file that have a version. If one wants to see all packages/classes from that
38 file, set the environment variable C<PERL_V_SHOW_ALL> to a I<true> value.
39  
40 If you want all available files/versions from C<@INC>:
41  
42 require V;
43 my @all_V = V::Module::Info->all_installed ("V");
44 printf "%s:\n", $all_V[0]->name;
45 for my $file (@all_V) {
46 my ($versions) = $file->version; # Must be list context
47 if (@$versions > 1) {
48 say "\t", $file->name;
49 print "\t %-30s: %s\n", $_->{pkg}, $_->{version} for @versions;
50 }
51 else {
52 printf "\t%-50s - %s\n", $file->file, $versions->[0]{version};
53 }
54 }
55  
56 Each element in that array isa C<V::Module::Info> object with 3 attributes and a method:
57  
58 =over
59  
60 =item I<attribute> B<name>
61  
62 The package name.
63  
64 =item I<attribute> B<file>
65  
66 Full filename with directory.
67  
68 =item I<attribute> B<dir>
69  
70 The base directory (from C<@INC>) where the package-file was found.
71  
72 =item I<method> B<version>
73  
74 This method will look through the file to see if it can find a version
75 assignment in the file and uses that to determine the version. As of version
76 B<0.13_01>, all versions found are passed through the L<version> module.
77  
78 As of version B<0.16_03> we look for all types of version declaration:
79  
80 package Foo;
81 our $VERSION = 0.42;
82  
83 and
84  
85 package Foo 0.42;
86  
87 and
88  
89 package Foo 0.42 { ... }
90  
91 Not only do we look for the C<package> keyword, but also for C<class>.
92 In list context this method will return an arrayref to a list of structures:
93  
94 =over 8
95  
96 =item I<pkg>
97  
98 The name of the C<package>/C<class>.
99  
100 =item I<version>
101  
102 The version for that C<package>/C<class>. (Can be absent if C<$PERL_V_SHOW_ALL>
103 is true.)
104  
105 =item I<ord>
106  
107 The ordinal number of occurrence in the file.
108  
109 =back
110  
111 =back
112  
113 =head1 DESCRIPTION
114  
115 This module uses stolen code from L<Module::Info> to find the location
116 and version of the specified module(s). It prints them and exit()s.
117  
118 It defines C<import ()> and is based on an idea from Michael Schwern
119 on the perl5-porters list. See the discussion:
120  
121 https://www.nntp.perl.org/group/perl.perl5.porters/2002/01/msg51007.html
122  
123 =head2 V::get_version($pkg)
124  
125 Returns the version of the first available file for this package as found by
126 following C<@INC>.
127  
128 =head3 Arguments
129  
130 =over
131  
132 =item 1. $pkg
133  
134 The name of the package for which one wants to know the version.
135  
136 =back
137  
138 =head3 Response
139  
140 This C<V::get_version ()> returns the version of the file that was first found
141 for this package by following C<@INC> or C<undef> if no file was found.
142  
143 =begin implementation
144  
145 =head2 report_pkg
146  
147 This sub prints the results for a package.
148  
149 =head3 Arguments
150  
151 =over
152  
153 =item 1. $pkg
154  
155 The name of the package that was probed for versions
156  
157 =item 2. @versions
158  
159 An array of Module-objects with full path and version.
160  
161 =back
162  
163 =end implementation
164  
165 =head1 SEE ALSO
166  
167 There are numerous module on CPAN that (try to) extract the VERSION
168 from modules. L<ExtUtils::MakeMaker> maybe being th most important
169 inspiration. L<Module::Info> was used to copy code from.
170  
171 =head1 AUTHOR
172  
173 Abe Timmerman -- 2002 - 2024 (✝ 2024-08-15 😢)
174 H.Merijn Brand C<< <hmbrand@cpan.org> >> (2026 ...)
175  
176 =head1 COPYRIGHT AND LICENSE
177  
178 Copyright 2024-2026 H.Merijn Brand, All Rights Reserved.
179 Copyright 2002-2024 Abe Timmerman, All Rights Reserved.
180  
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.
183  
184 This program is distributed in the hope that it will be useful,
185 but WITHOUT ANY WARRANTY; without even the implied warranty of
186 MERCHANT-ABILITY or FITNESS FOR A PARTICULAR PURPOSE.
187  
188 =cut
189  
190 2 ▶sub report_pkg ($@) {
Subroutine report_pkg: called CC=7 SCAR=19.8
Pod documented
191 2 my $pkg = shift;
192  
193 2 print "$pkg\n";
194 2 ▶ @_ or print "\tNot found\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ or print "\tNot found\n" 2 0 2
195 2 foreach my $module (@_) {
196 3 my ($versions) = $module->version;
197 3 ▶ if (@$versions > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$versions > 1) { } 1 2 3
198 1 printf "\t%s:\n", $module->file;
199 1 ▶ printf "\t %s: %s\n", $_->{pkg}, $_->{version} || '' for @$versions;
Condition50%1/2
$_->{'version'} || ''
l!l
30
MC/DC0%0/1
$_->{'version'} || ''
$_->{'version'}
Truth table50%1/2
A
A= $_->{'version'}
Aresult
11
00
200 }
201 else {
202 2 ▶ printf "\t%s: %s\n", $module->file, $versions->[0]{version} || '?';
Condition50%1/2
$versions->[0]{'version'} || '?'
l!l
20
MC/DC0%0/1
$versions->[0]{'version'} || '?'
$versions->[0]{'version'}
Truth table50%1/2
A
A= $versions->[0]{'version'}
Aresult
11
00
203 }
204 }
205 } # report_pkg
206  
207 2 ▶sub import {
Subroutine import: called CC=4 SCAR=14.2
208 2 shift;
209 2 ▶ @_ or push @_ => 'V';
Branch: 1 branch
BranchTrueFalseTotal
@_ or push @_, "V" 2 0 2
210  
211 2 for my $pkg (@_) {
212 2 my @modules = V::Module::Info->all_installed ($pkg);
213 2 report_pkg $pkg, @modules;
214 }
215 2 ▶ $NO_EXIT or exit ();
Branch: 1 branch
BranchTrueFalseTotal
$V::NO_EXIT or exit() 2 0 2
216 } # import
217  
218 10 ▶sub get_version {
Subroutine get_version: called CC=2 SCAR=7.1
Pod documented
219 10 my ($pkg) = @_;
220 10 my ($first) = V::Module::Info->all_installed ($pkg);
221 10 ▶ return $first ? $first->version : undef;
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 10 0 10
222 } # get_version
223  
224 8 ▶caller or V->import (@ARGV);
Branch: 1 branch
BranchTrueFalseTotal
caller or "V"->import(@ARGV) 8 0 8
225  
226 8 1;
227  
228 # Okay I did the AUTOLOAD bit, but this is a Copy 'n Paste job.
229 # Thank you Michael Schwern for Module::Info! This one is mostly that!
230  
231 package V::Module::Info;
232  
233 8 require File::Spec;
234  
235 14 ▶sub new_from_file {
Subroutine new_from_file: called CC=3 SCAR=12.5
236 14 my ($proto, $file) = @_;
237 14 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
238  
239 14 ▶ -r $file and return bless {
Branch: 1 branch
BranchTrueFalseTotal
-r $file and return bless({"file", "File::Spec"->rel2abs($file), "dir", "", "name", ""}, $class) 14 0 14
240 file => File::Spec->rel2abs ($file),
241 dir => "",
242 name => "",
243 } => $class;
244 } # new_from_file
245  
246 12 ▶sub all_installed {
Subroutine all_installed: called CC=8 SCAR=22.3
247 12 my ($proto, $name, @inc) = @_;
248 12 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
249  
250 12 ▶ @inc or @inc = @INC;
Branch: 1 branch
BranchTrueFalseTotal
@inc or @inc = @INC 0 12 12
251 12 my $file = File::Spec->catfile (split m/::/ => $name) . ".pm";
252  
253 12 my @modules;
254 12 foreach my $dir (@inc) {
255 # Skip the new code ref in @INC feature.
256 89 ▶ ref $dir and next;
Branch: 1 branch
BranchTrueFalseTotal
ref $dir and next 0 89 89
257  
258 89 my $filename = File::Spec->catfile ($dir, $file);
259 89 ▶ -r $filename or next;
Branch: 1 branch
BranchTrueFalseTotal
-r $filename or next 14 75 89
260  
261 14 my $module = $class->new_from_file ($filename);
262 14 $module->{dir} = File::Spec->rel2abs ($dir);
263 14 $module->{name} = $name;
264 14 push @modules => $module;
265 }
266  
267 0 ▶ $V::DEBUG and do { print {*STDERR} "# $file: @{[scalar $_->version]}\n" for @modules };
Branch: 1 branch
BranchTrueFalseTotal
$V::DEBUG and do { print {*STDERR;} "# ${file}: @{[scalar $_->version];}\n" foreach (@modules) } 0 12 12
268 12 return @modules;
269 } # all_installed
270  
271 # Once thieved from ExtUtils::MM_Unix 1.12603
272 # Stealing from Module::Extract::VERSION is an option for the future
273 13 ▶sub version {
Subroutine version: called CC=29 SCAR=34.2
274 13 my $self = shift;
275  
276 13 my $parsefile = $self->file;
277  
278 13 ▶ open my $mod, "<", $parsefile or die "open $parsefile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $mod, "<", $parsefile or die "open ${parsefile}: $!" 13 0 13
279  
280 13 my $inpod = 0;
281 13 local $_;
282 13 my %eval;
283 13 my ($cur_pkg, $cur_ord) = ("main", 0);
284 13 $eval{$cur_pkg} = { ord => $cur_ord };
285 13 ▶ while (<$mod>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $mod)) 2172 13 2185
286 2172 ▶ $inpod = m/^=(?!cut)/ ? 1 : m/^=cut/ ? 0 : $inpod;
Branch: 2 branches
BranchTrueFalseTotal
/^=(?!cut)/ ? : 125 2047 2172
/^=cut/ ? : 5 2042 2047
287 2172 ▶ $inpod || m/^\s*#/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$inpod || /^\s*#/ and next 827 1345 2172
Condition100%3/3
$inpod or /^\s*#/
l!l&&r!l&&!r
6971301345
MC/DC100%2/2
$inpod or /^\s*#/
$inpod /^\s*#/
Truth table100%3/3
A or B
A= $inpodB= /^\s*#/
ABresult
1X1
011
000
288  
289 1345 chomp;
290 1345 ▶ if (m/^\s* (?:package|class) \s+ (\w+(?:::\w+)*) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s* (?:package|class) \s+ (\w+(?:::\w+)*) /x) 24 1321 1345
291 24 $cur_pkg = $1;
292 exists $eval{$cur_pkg} or
293 24 ▶ $eval{$cur_pkg} = { ord => ++$cur_ord };
Branch: 1 branch
BranchTrueFalseTotal
exists $eval{$cur_pkg} or $eval{$cur_pkg} = {"ord", ++$cur_ord} 0 24 24
294 }
295  
296 1345 ▶ $cur_pkg =~ m{^V::Module::Info} and next;
Branch: 1 branch
BranchTrueFalseTotal
$cur_pkg =~ /^V::Module::Info/ and next 459 886 1345
297  
298 886 ▶ if (m/(?:our)?\s*([\$*])(([\w\:\']*)\bVERSION)\s*\=(?![=~])/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/(?:our)?\s*([\$*])(([\w\:\']*)\bVERSION)\s*\=(?![=~])/) { } 11 875 886
elsif (/^\s* (?:package|class) \s+ [^\s]+ \s+ (v?[0-9.]+) \s* [;\{]/x) { } 7 868 875
299 11 { local ($1, $2); ($_ = $_) = m/(.*)/; } # untaint
300 11 my ($sigil, $name) = ($1, $2);
301 11 ▶ m/\$$name\s*=\s*eval.+\$$name/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/\$$name\s*=\s*eval.+\$$name/ and next 0 11 11
302 11 ▶ m/my\s*\$VERSION\s*=/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/my\s*\$VERSION\s*=/ and next 0 11 11
303 11 ▶ m/^[^']*'[^']*\$$name[^']*'/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^[^']*'[^']*\$${name}[^']*'/ and next 1 10 11
304 10 $eval{$cur_pkg}{prg} = qq{
305 package V::Module::Info::_version_var;
306 # $cur_pkg
307 no strict;
308 local $sigil$name;
309 \$$name = undef;
310 do { $_
311 # Closing brace needs to be on next line
312 # as toping can haz comment
313 };
314 \$$name
315 };
316 }
317 # perl 5.12.0+
318 elsif (m/^\s* (?:package|class) \s+ [^\s]+ \s+ (v?[0-9.]+) \s* [;\{]/x) {
319 7 my $ver = $1;
320 7 ▶ if ($] >= 5.012000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.012) { } 7 0 7
321 7 $eval{$cur_pkg}{prg} = qq{
322 package V::Module::Info::_version_static $ver;
323 # $cur_pkg
324 V::Module::Info::_version_static->VERSION;
325 };
326 }
327 else {
328 0 warn "Your perl doesn't understand the version declaration of $cur_pkg\n";
329 0 $eval{$cur_pkg}{prg} = qq{ $ver };
330 }
331 }
332 }
333 13 close $mod;
334  
335 # remove our stuff
336 13 delete $eval{$_} for grep { m/^V::Module::Info/ } keys %eval;
337  
338 13 my @results;
339 13 ▶ while (my ($pkg, $dat) = each %eval) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %eval) 28 13 41
340 28 my $result;
341 28 ▶ if ($dat->{prg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dat->{"prg"}) 17 11 28
342 17 ▶ $V::DEBUG and warn "# $pkg: $dat->{prg}\n";
Branch: 1 branch
BranchTrueFalseTotal
$V::DEBUG and warn qq[# ${pkg}: $dat->{"prg"}\n] 0 17 17
343 17 local $^W = 0;
344 17 ▶ $result = eval $dat->{prg};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
345 17 ▶ $V::DEBUG && $@ and warn "Could not eval '$dat->{prg}' in $parsefile: $@";
Branch: 1 branch
BranchTrueFalseTotal
$V::DEBUG and $@ and warn qq[Could not eval '$dat->{"prg"}' in ${parsefile}: $@] 0 17 17
Condition33%1/3
$V::DEBUG and $@
!ll&&!rl&&r
1700
MC/DC0%0/2
$V::DEBUG and $@
$V::DEBUG $@
Truth table33%1/3
A and B
A= $V::DEBUGB= $@
ABresult
0X0
100
111
346  
347 # use the version modulue to deal with v-strings
348 17 require version;
349 17 $dat->{ver} = $result = version->parse ($result);
350 }
351 push @results => {
352 (exists $dat->{ver} ? (version => $result) : ()),
353 pkg => $pkg,
354 ord => $dat->{ord},
355 28 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
exists $dat->{'ver'} ? : 17 11 28
356 }
357 $ENV{PERL_V_SHOW_ALL} or
358 13 ▶ @results = grep { exists ($_->{version}) } @results;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PERL_V_SHOW_ALL"} or @results = grep({exists $_->{"version"};} @results) 0 13 13
359  
360 @results > 1 and
361 13 ▶ @results = grep { $_->{pkg} ne "main" || exists $_->{version} } @results;
Branch: 1 branch
BranchTrueFalseTotal
@results > 1 and @results = grep({$_->{"pkg"} ne "main" or exists $_->{"version"};} @results) 2 11 13
Condition66%2/3
$_->{'pkg'} ne 'main' || exists $_->{'version'}
l!l&&r!l&&!r
420
MC/DC0%0/2
$_->{'pkg'} ne 'main' || exists $_->{'version'}
$_->{'pkg'} ne 'main' exists $_->{'version'}
Truth table66%2/3
A || B
A= $_->{'pkg'} ne 'main'B= exists $_->{'version'}
ABresult
1X1
011
000
362  
363 13 ▶ unless (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 10 3 13
364 10 foreach my $option (@results) {
365 10 ▶ $option->{pkg} eq $self->name or next;
Branch: 1 branch
BranchTrueFalseTotal
$option->{"pkg"} eq $self->name or next 10 0 10
366 10 return $option->{version};
367 }
368 0 return;
369 }
370 3 return [ sort { $a->{ord} <=> $b->{ord} } @results ];
371 } # version
372  
373 26 ▶sub accessor {
Subroutine accessor: called CC=2 SCAR=7.0
374 26 my $self = shift;
375 26 my $field = shift;
376  
377 26 ▶ @_ and $self->{$field} = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{$field} = $_[0] 0 26 26
378 26 return $self->{$field};
379 } # accessor
380  
381 40 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
382 40 my ($self) = @_;
383  
384 8 ▶ use vars qw( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
385 40 my ($method) = $AUTOLOAD =~ m{.+::(.+)$};
386  
387 40 ▶ if (exists $self->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}) 26 14 40
388 26 splice @_, 1, 0, $method;
389 26 goto &accessor;
390 }
391 } # AUTOLOAD
392  
393 8 1;
↑ Summary