File Coverage

script/perlvars
Criterion Covered Total %
statement 31 36 86.1
branch 10 12 83.3
condition n/a
subroutine 5 5 100.0
pod n/a
total 46 53 86.7


line stmt bran cond sub pod time code
1             #!perl
2              
3 12     12   70655 use strict;
  12         20  
  12         486  
4 12     12   53 use warnings;
  12         14  
  12         701  
5 12     12   167 use v5.14;
  12         41  
6              
7 12     12   6158 use App::perlvars ();
  12         58  
  12         424  
8 12     12   7921 use Getopt::Long::Descriptive qw( describe_options );
  12         506809  
  12         114  
9              
10 12         5675718 my ( $opt, $usage ) = describe_options(
11             'perlvars %o file',
12             [ 'ignore-file|i=s', 'A file containing an ignore list', ],
13             [],
14             [ 'help', 'print usage message and exit', { shortcircuit => 1 } ],
15             [
16             'verbose-help', 'print verbose usage message and exit',
17             { shortcircuit => 1 }
18             ],
19             );
20              
21 12 50       25264 if ( $opt->help ) {
22 0         0 say( $usage->text );
23 0         0 exit;
24             }
25              
26 12         150 my $exit_code = 0;
27              
28 12         114 my @files = @ARGV;
29              
30 12 50       51 unless (@files) {
31 0         0 require Pod::Usage;
32 0         0 say $usage->text;
33 0         0 Pod::Usage::pod2usage();
34             }
35              
36 12 100       47 my $vars = App::perlvars->new(
37             $opt->ignore_file ? ( ignore_file => $opt->ignore_file ) : () );
38              
39 12         47 for my $file (@files) {
40 14         1242 say $file;
41 14         120 my ( $code, $error_message, @notes ) = $vars->validate_file($file);
42              
43 9 100       27002 if ($error_message) {
44 3         348 say STDERR $error_message;
45 3         380 exit($code);
46             }
47              
48 6 100       68 if ( $code > 0 ) {
49 1         7 $exit_code = $code;
50 1         236 say STDERR $_ for @notes;
51             }
52             }
53              
54 4 100       284 say 'All files ok' unless $exit_code;
55              
56 4         1582 exit($exit_code);
57              
58             # PODNAME: perlvars
59             # ABSTRACT: A command line utility for detecting unused Perl variables
60              
61             __END__
62              
63             =pod
64              
65             =encoding UTF-8
66              
67             =head1 NAME
68              
69             perlvars - A command line utility for detecting unused Perl variables
70              
71             =head1 VERSION
72              
73             version 0.000006
74              
75             =head1 SYNOPSIS
76              
77             Detect unused variables in Perl code.
78              
79             perlvars lib/Foo.pm lib/Foo/Bar.pm
80              
81             PERL5OPT="-I." perlvars Foo.pm Baz.pm
82              
83             You can also ignore arbitrary variables on a per-package basis, using an ignore
84             file.
85              
86             perlvars --ignore-file ignore-list.txt lib/Foo.pm lib/Foo/Bar.pm
87              
88             See the documentation for L<App::perlvars> for the format of the ignore file.
89              
90             If you'd like to check every .pm file in your lib directory, you can try
91             something like:
92              
93             find lib | grep pm$ | xargs perlvars
94              
95             =head1 DESCRIPTION
96              
97             This script (which is based heavily on the code in
98             L<Code::TidyAll::Plugin::Test::Vars>) is a wrapper around L<Test::Vars>, which
99             tries to find unused variables in your Perl code. Please note that since
100             L<Test::Vars> only finds unused variables contained within packages, code which
101             does not have an explicit package name will not be checked.
102              
103             =head1 CAVEATS
104              
105             As noted above, there are some serious limitations to this script, due to the
106             way that L<Test::Vars> works. You're strongly encouraged to consider using
107             L<Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter> if that's a
108             possibility for you.
109              
110             Your code needs an explicit package name.
111              
112             package Foo::Bar;
113             ...
114             1;
115              
116             The package name needs to match the file name, so the package above needs to be in a file named Foo/Bar.pm.
117              
118             The package needs be in C<@INC> or in a C<./lib> folder. This means that for the example above, either of these should work:
119              
120             perlvars lib/Foo/Bar.pm
121              
122             cd lib && PERL5OPT="-I." perlvars Foo/Bar.pm
123              
124             =head1 ACKNOWLEDGEMENTS
125              
126             The code in this module is largely copied directly from L<Code::TidyAll::Plugin::Test::Vars>.
127              
128             =head1 SEE ALSO
129              
130             You may also wish to use
131             L<Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter> which can find
132             some cases which L<Test::Vars> is not able to detect. It also does not require
133             the code to be inside a package.
134              
135             =head1 AUTHOR
136              
137             Olaf Alders <olaf@wundercounter.com>
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             This software is copyright (c) 2022 by MaxMind, Inc.
142              
143             This is free software; you can redistribute it and/or modify it under
144             the same terms as the Perl 5 programming language system itself.
145              
146             =cut