-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrintUsed.pm
More file actions
128 lines (82 loc) · 3.19 KB
/
PrintUsed.pm
File metadata and controls
128 lines (82 loc) · 3.19 KB
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
package Module::PrintUsed;
use 5.006;
use strict;
use warnings;
our @ISA = qw();
our $VERSION = '0.06';
sub ModulesList {
my @modules;
foreach (sort keys %INC) {
my $name = $_;
$name =~ s|[\\/]|::|g;
$name =~ s|\.pm$||;
my $version;
{
# Prevent warnings caused by autosplitted modules.
# See RT#48573.
no warnings;
$version = eval " \$$name\::VERSION " || '';
}
push @modules, { name => $name, version => $version,
path => $INC{$_} };
}
return @modules;
}
sub FormattedModulesList {
my $text = "\nModules used by $0:\n";
my @modules = ModulesList();
foreach (@modules) {
my $lengthName = 25 - length($_->{name});
$lengthName = 0 unless $lengthName > 0;
my $lengthVersion = 8 - length($_->{version});
$lengthVersion = 0 unless $lengthVersion > 0;
$text .= " - $_->{name} " . (" " x $lengthName) .
"$_->{version} " . (" " x $lengthVersion) .
"$_->{path}\n";
}
$text .= "\n";
return $text;
}
END {
print STDERR FormattedModulesList();
}
1;
__END__
=encoding UTF-8
=head1 NAME
Module::PrintUsed - Prints modules used by your script when your script ends
=head1 SYNOPSIS
use Module::PrintUsed;
=head1 DESCRIPTION
This module helps you to check which modules (and scripts) were C<use>d or C<require>d during the runtime of your script. It prints the list of modules to STDERR, including version numbers and paths.
Module::PrintUsed contains an C<END {}> block that will be executed when your script exits (even if it died).
=head2 USAGE VIA PERL5OPT
It is possible to print a list of modules used even without modifying your perl scripts or programs. To achieve this, set the PERL5OPT environment variable to "-MModule::PrintUsed".
Unix command-line example:
env PERL5OPT=-MModule::PrintUsed perl myscript.pl
Windows command-line example:
set PERL5OPT=-MModule::PrintUsed
perl myscript.pl
=head1 FUNCTIONS
=over 4
=item C<Module::PrintUsed::ModulesList()>
Returns a list of modules used in the format
@modules = ({name => 'Some::Module', version => '0.1',
path => '/home/thisuser/lib/Some/Module.pm'}, ...);
=item C<Module::PrintUsed::FormattedModulesList()>
Returns a scalar that contains a pretty-printed version of the
modules list.
=back
=head1 DEVELOPMENT NOTES
Please report any bugs sing the CPAN RT system. The development repository for this module is hosted on GitHub: L<http://github.com/crenz/Module-PrintUsed/>.
=head1 THANKS
Thanks to Slaven Rezić for pointing out that Module::PrintUsed can be used with the PERL5OPT environment variable, and for alerting me to warnings caused by autosplitted modules.
=head1 SEE ALSO
A more sophisticated way of finding module dependencies without having
to execute the script is performed by L<Module::ScanDeps>.
=head1 AUTHOR
Christian Renz, E<lt>crenz @ web42.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2004-2015 Christian Renz E<lt>crenz @ web42.comE<gt>. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.