File Coverage

blib/lib/Devel/VersionDump.pm
Criterion Covered Total %
statement 32 38 84.2
branch 9 16 56.2
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 48 61 78.6


line stmt bran cond sub pod time code
1             package Devel::VersionDump;
2              
3 1     1   22179 use strict;
  1         2  
  1         33  
4 1     1   4 use warnings;
  1         2  
  1         364  
5              
6             our $VERSION = '0.02';
7              
8             =head1 NAME
9              
10             Devel::VersionDump - Dump loaded module versions to the console
11              
12             =head1 VERSION
13              
14             0.02
15              
16             =head1 SYNOPSIS
17              
18             perl -MDevel::VersionDump your-script.pl
19              
20             use Devel::VersionDump;
21              
22             use Devel::VersionDump '-stderr';
23              
24             use Devel::VersionDump qw(dump_versions);
25             # later...
26             dump_versions;
27              
28             =head1 DESCRIPTION
29              
30             This module prints a sorted list of modules used by
31             your program to stdout. It does this by walking C<%INC>
32             in Perl's C phase.
33              
34             =head1 IMPORT OPTIONS
35              
36             =over
37              
38             =item -stderr
39              
40             Tells the dumper to print to stderr instead of stdout.
41              
42             =item dump_versions
43              
44             Exports the C function into the caller's namespace,
45             and turns off the automatic printing in the C phase. Dumping
46             versions is then achieved by calling C.
47              
48             =back
49              
50             =head1 FUNCTIONS
51              
52             =cut
53              
54             my $stderr = 0;
55             my $automatic = 1;
56              
57             =head2 dump_versions
58              
59             Dumps versions to STDOUT or STDERR, depending on if '-stderr' was
60             specified in import.
61              
62             =cut
63              
64             sub dump_versions {
65 1     1 1 4 my $oldfh;
66              
67 1 50       5 $oldfh = select(STDERR) if $stderr;
68              
69 1         62 print "Perl version: $^V on $^O ($^X)\n";
70 1         4 my @modules;
71 1         3 my ($max_mod_len, $max_vers_len) = (0, 0);
72 1         66 foreach my $module (sort keys %INC) {
73 71         232 $module =~ s/\//::/g;
74 71         317 $module =~ s/\.pm$//;
75              
76 71         3890 my $version = eval "\$${module}::VERSION";
77 71 100       268 $version = 'Unknown' unless defined $version;
78 71         196 push @modules, [$module, $version];
79              
80 71 100       191 if(length($module) > $max_mod_len) {
81 7         10 $max_mod_len = length($module);
82             }
83 71 100       246 if(length($version) > $max_vers_len) {
84 3         6 $max_vers_len = length($version);
85             }
86             }
87 1         11 my $format = "%-${max_mod_len}s - %${max_vers_len}s\n";
88 1         4 foreach my $pair (@modules) {
89 71         111 my ($module, $version) = @$pair;
90 71         478 printf $format, $module, $version;
91             }
92              
93 1 50       25 select($oldfh) if $stderr;
94             }
95              
96             sub import {
97 1     1   8 shift;
98 1         10 foreach (@_) {
99 0 0         if($_ eq '-stderr') {
    0          
100 0           $stderr = 1;
101             } elsif($_ eq 'dump_versions') {
102 0           $automatic = 0;
103 0           my $pkg = caller;
104 1     1   6 no strict 'refs';
  1         5  
  1         94  
105 0           *{$pkg . '::dump_versions'} = \&dump_versions;
  0            
106             }
107             }
108             }
109              
110             INIT {
111 1 50   1   7 dump_versions if $automatic;
112             }
113              
114             =head1 AUTHOR
115              
116             Rob Hoelz
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests to C, or through
121             the web interface at L. I will
122             be notified, and then you'll automatically be notified of progress on your bug as I make changes.
123              
124             =head1 COPYRIGHT & LICENSE
125              
126             Copyright 2011 Rob Hoelz.
127              
128             This module is free software; you can redistribute it and/or modify it under
129             the same terms as Perl itself.
130              
131             =cut
132              
133             1;