9 perl -MO=Lint[,OPTIONS] foo.pl
13 The B::Lint module is equivalent to an extended version of the B<-w>
14 option of B<perl>. It is named after the program B<lint> which carries
15 out a similar process for C programs.
17 =head1 OPTIONS AND LINT CHECKS
19 Option words are separated by commas (not whitespace) and follow the
20 usual conventions of compiler backend options. Following any options
21 (indicated by a leading B<->) come lint check arguments. Each such
22 argument (apart from the special B<all> and B<none> options) is a
23 word representing one possible lint check (turning on that check) or
24 is B<no-foo> (turning off that check). Before processing the check
25 arguments, a standard list of checks is turned on. Later options
26 override earlier ones. Available options are:
32 Produces a warning whenever an array is used in an implicit scalar
33 context. For example, both of the lines
37 will elicit a warning. Using an explicit B<scalar()> silences the
42 =item B<implicit-read> and B<implicit-write>
44 These options produce a warning whenever an operation implicitly
45 reads or (respectively) writes to one of Perl's special variables.
46 For example, B<implicit-read> will warn about these:
50 and B<implicit-write> will warn about these:
54 Both B<implicit-read> and B<implicit-write> warn about this:
58 =item B<dollar-underscore>
60 This option warns whenever $_ is used either explicitly anywhere or
61 as the implicit argument of a B<print> statement.
63 =item B<private-names>
65 This option warns on each use of any variable, subroutine or
66 method name that lives in a non-current package but begins with
67 an underscore ("_"). Warnings aren't issued for the special case
68 of the single character name "_" by itself (e.g. $_ and @_).
70 =item B<undefined-subs>
72 This option warns whenever an undefined subroutine is invoked.
73 This option will only catch explicitly invoked subroutines such
74 as C<foo()> and not indirect invocations such as C<&$subref()>
75 or C<$obj-E<gt>meth()>. Note that some programs or modules delay
76 definition of subs until runtime by means of the AUTOLOAD
79 =item B<regexp-variables>
81 This option warns whenever one of the regexp variables $', $& or
82 $' is used. Any occurrence of any of these variables in your
83 program can slow your whole program down. See L<perlre> for
92 Turn all warnings off.
96 =head1 NON LINT-CHECK OPTIONS
102 Normally, Lint only checks the main code of the program together
103 with all subs defined in package main. The B<-u> option lets you
104 include other package names whose subs are then checked by Lint.
110 This is only a very preliminary version.
114 Malcolm Beattie, mbeattie@sable.ox.ac.uk.
119 use B
qw(walkoptree main_root walksymtable svref_2object parents
120 OPf_WANT_LIST OPf_WANT OPf_STACKED G_ARRAY
123 my $file = "unknown"; # shadows current filename
124 my $line = 0; # shadows current line number
125 my $curstash = "main"; # shadows current stash
129 my %implies_ok_context;
131 map($implies_ok_context{$_}++,
132 qw(scalar av2arylen aelem aslice helem hslice
133 keys values hslice defined undef delete));
136 # Lint checks turned on by default
137 my @default_checks = qw(context);
142 map($valid_check{$_}++,
143 qw(context implicit_read implicit_write dollar_underscore
144 private_names undefined_subs regexp_variables));
150 my %done_cv; # used to mark which subs have already been linted
151 my @extra_packages; # Lint checks mainline code and all subs which are
152 # in main:: or in one of these packages.
155 my $format = (@_ < 2) ?
"%s" : shift;
156 warn sprintf("$format at %s line %d\n", @_, $file, $line);
159 # This gimme can't cope with context that's only determined
160 # at runtime via dowantarray().
163 my $flags = $op->flags;
164 if ($flags & OPf_WANT
) {
165 return(($flags & OPf_WANT_LIST
) ?
1 : 0);
174 if ($op->name eq "nextstate") {
177 $curstash = $op->stash->NAME;
183 my $opname = $op->name;
184 if ($check{context
} && ($opname eq "rv2av" || $opname eq "rv2hv")) {
185 my $parent = parents
->[0];
186 my $pname = $parent->name;
187 return if gimme
($op) || $implies_ok_context{$pname};
188 # Two special cases to deal with: "foreach (@foo)" and "delete $a{$b}"
189 # null out the parent so we have to check for a parent of pp_null and
190 # a grandparent of pp_enteriter or pp_delete
191 if ($pname eq "null") {
192 my $gpname = parents
->[1]->name;
193 return if $gpname eq "enteriter" || $gpname eq "delete";
195 warning
("Implicit scalar context for %s in %s",
196 $opname eq "rv2av" ?
"array" : "hash", $parent->desc);
198 if ($check{private_names
} && $opname eq "method") {
199 my $methop = $op->first;
200 if ($methop->name eq "const") {
201 my $method = $methop->sv->PV;
202 if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
203 warning
("Illegal reference to private method name $method");
211 if ($check{implicit_read
}) {
212 if ($op->name eq "match" && !($op->flags & OPf_STACKED
)) {
213 warning
('Implicit match on $_');
216 if ($check{implicit_write
}) {
217 if ($op->name eq "subst" && !($op->flags & OPf_STACKED
)) {
218 warning
('Implicit substitution on $_');
225 if ($check{implicit_read
} || $check{implicit_write
}) {
226 if ($op->name eq "enteriter") {
227 my $last = $op->last;
228 if ($last->name eq "gv" && $last->gv->NAME eq "_") {
229 warning
('Implicit use of $_ in foreach');
237 if ($check{dollar_underscore
} && $op->name eq "gvsv"
238 && $op->gv->NAME eq "_")
240 warning
('Use of $_');
242 if ($check{private_names
}) {
243 my $opname = $op->name;
244 if ($opname eq "gv" || $opname eq "gvsv") {
246 if ($gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash) {
247 warning
('Illegal reference to private name %s', $gv->NAME);
251 if ($check{undefined_subs
}) {
252 if ($op->name eq "gv"
253 && $op->next->name eq "entersub")
256 my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
258 if (!defined(&$subname)) {
259 $subname =~ s/^main:://;
260 warning
('Undefined subroutine %s called', $subname);
264 if ($check{regexp_variables
} && $op->name eq "gvsv") {
265 my $name = $op->gv->NAME;
266 if ($name =~ /^[&'`]$/) {
267 warning
('Use of regexp variable $%s', $name);
275 #warn sprintf("lintcv: %s::%s (done=%d)\n",
276 # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
277 return if !$$cv || $done_cv{$$cv}++;
278 my $root = $cv->ROOT;
279 #warn " root = $root (0x$$root)\n";#debug
280 walkoptree
($root, "lint") if $$root;
285 walkoptree
(main_root
, "lint") if ${main_root
()};
287 # Now do subs in main
288 no strict
qw(vars refs);
291 while (($sym, *glob) = each %{"main::"}) {
292 #warn "Trying $sym\n";#debug
293 svref_2object
(\
*glob)->EGV->lintcv unless $sym =~ /::$/;
296 # Now do subs in non-main packages given by -u options
297 map { $search_pack{$_} = 1 } @extra_packages;
298 walksymtable
(\
%{"main::"}, "lintcv", sub {
301 #warn "Considering $package\n";#debug
302 return exists $search_pack{$package};
308 my ($option, $opt, $arg);
309 # Turn on default lint checks
310 for $opt (@default_checks) {
314 while ($option = shift @options) {
315 if ($option =~ /^-(.)(.*)/) {
319 unshift @options, $option;
322 if ($opt eq "-" && $arg eq "-") {
325 } elsif ($opt eq "D") {
326 $arg ||= shift @options;
327 foreach $arg (split(//, $arg)) {
330 } elsif ($arg eq "O") {
334 } elsif ($opt eq "u") {
335 $arg ||= shift @options;
336 push(@extra_packages, $arg);
339 foreach $opt (@default_checks, @options) {
342 %check = %valid_check;
344 elsif ($opt eq "none") {
348 if ($opt =~ s/^no-//) {
354 warn "No such check: $opt\n" unless defined $valid_check{$opt};
357 # Remaining arguments are things to check