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