File Coverage

blib/lib/Module/Version/Loaded.pm
Criterion Covered Total %
statement 30 35 85.7
branch n/a
condition 3 8 37.5
subroutine 8 9 88.8
pod 4 4 100.0
total 45 56 80.3


line stmt bran cond sub pod time code
1 1     1   834390 use strict;
  1         1  
  1         36  
2 1     1   5 use warnings;
  1         2  
  1         63  
3             package Module::Version::Loaded;
4              
5             our $VERSION = '0.000002';
6              
7 1     1   474 use Module::Version 0.12 qw( get_version );
  1         73586  
  1         69  
8 1         6 use Sub::Exporter -setup => {
9             exports => [
10             'diff_versioned_modules',
11             'store_versioned_modules',
12             'versioned_inc',
13             'versioned_modules',
14             ]
15 1     1   447 };
  1         6508  
16              
17             sub versioned_inc {
18 1     1 1 6 my %versioned;
19 1         48 foreach my $module ( keys %INC ) {
20 110         165 my ( $version, undef ) = _module_version($module);
21 110         50966 $versioned{$module} = $version;
22             }
23 1         148 return %versioned;
24             }
25              
26             sub versioned_modules {
27 3     3 1 16 my %versioned;
28 3         87 foreach my $file ( keys %INC ) {
29 332         568 my ( $version, $module ) = _module_version($file);
30 332         183684 $versioned{$module} = $version;
31             }
32 3         291 return %versioned;
33             }
34              
35             sub diff_versioned_modules {
36 0   0 0 1 0 my $file1 = shift || '2 file names required';
37 0   0     0 my $file2 = shift || '2 file names required';
38              
39 0         0 require Data::Difflet;
40 0         0 require Storable;
41              
42 0         0 print Data::Difflet->new->compare(
43             Storable::retrieve($file1),
44             Storable::retrieve($file2)
45             );
46             }
47              
48             sub store_versioned_modules {
49 2   50 2 1 46261 my $file = shift || die 'file name required';
50 2         7 my %versioned = versioned_modules();
51              
52 2         614 require Storable;
53 2         2360 Storable::nstore( \%versioned, $file );
54             }
55              
56             sub _module_version {
57 442     442   551 my $module = shift;
58 442         1054 $module =~ s{/}{::}g;
59 442         1098 $module =~ s{\.pm\z}{};
60 442   100     1007 return ( get_version($module) || undef, $module );
61             }
62             1;
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =head1 NAME
69              
70             Module::Version::Loaded - Get a versioned list of currently loaded modules
71              
72             =head1 VERSION
73              
74             version 0.000003
75              
76             =head1 SYNOPSIS
77              
78             use Module::Version::Loaded qw( versioned_modules );
79              
80             my %modules = versioned_modules();
81             # %modules contains: ( Foo::Bar => 0.01, Bar::Foo => 1.99, ... )
82              
83             # You can test this with a series of one-liners
84             perl -MModule::Version::Loaded=store_versioned_modules -MTest::More -e "store_versioned_modules('test-more.txt')"
85             perl -MModule::Version::Loaded=store_versioned_modules -MApp::Prove -e "store_versioned_modules('app-prove.txt')"
86             perl -MModule::Version::Loaded=diff_versioned_modules -e "diff_versioned_modules('test-more.txt','app-prove.txt')"
87              
88             =head1 DESCRIPTION
89              
90             BETA BETA BETA
91              
92             This module exists solely to give you a version of your %INC which includes the
93             versions of the modules you have loaded. This is helpful when troubleshooting
94             different environments. It makes it easier to see, at glance, which versions
95             of modules you have actually loaded.
96              
97             =head1 FUNCTIONS
98              
99             =head2 versioned_modules
100              
101             Returns a C of module versions, which is keyed on module name.
102              
103             use Module::Version::Loaded qw( versioned_modules );
104             my %modules = versioned_modules();
105             # contains:
106             ...
107             vars => 1.03,
108             version => 0.9912,
109             version::regex => 0.9912,
110             version::vxs => 0.9912,
111             ...
112              
113             =head2 versioned_inc
114              
115             Returns a C of module versions, which uses the same keys as %INC. This
116             makes it easier to compare this data which %INC, since both Hashes will share
117             the same keys.
118              
119             use Module::Version::Loaded qw( versioned_inc );
120             my %inc = versioned_inc();
121             # contains:
122             ...
123             version.pm => 0.9912,
124             version/regex.pm => 0.9912,
125             version/vxs.pm => 0.9912,
126             warnings.pm => 1.18,
127             ...
128              
129             foreach my $key ( %INC ) {
130             print "$key $INC{$key} $inc{$key}\n";
131             }
132              
133             =head2 store_versioned_modules( $file )
134              
135             Serializes your versioned module list to an arbitrary file name which you must
136             provide.
137              
138             =head2 diff_versioned_modules( $file1, $file2 )
139              
140             Requires the name of two files, which have previously been serialized via
141             C. Uses C to print a comparison of
142             these data structures to STDOUT.
143              
144             You can do this with a one-line to save time:
145              
146             perl -MModule::Version::Loaded=diff_versioned_modules -e "diff_versioned_modules('file1','file2')"
147              
148             =head1 AUTHOR
149              
150             Olaf Alders
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is Copyright (c) 2015 by MaxMind, Inc..
155              
156             This is free software, licensed under:
157              
158             The Artistic License 2.0 (GPL Compatible)
159              
160             =cut
161              
162             __END__