11 perl -MO=Lint[,OPTIONS] foo.pl
15 The B::Lint module is equivalent to an extended version of the B<-w>
16 option of B<perl>. It is named after the program B<lint> which carries
17 out a similar process for C programs.
19 =head1 OPTIONS AND LINT CHECKS
21 Option words are separated by commas (not whitespace) and follow the
22 usual conventions of compiler backend options. Following any options
23 (indicated by a leading B<->) come lint check arguments. Each such
24 argument (apart from the special B<all> and B<none> options) is a
25 word representing one possible lint check (turning on that check) or
26 is B<no-foo> (turning off that check). Before processing the check
27 arguments, a standard list of checks is turned on. Later options
28 override earlier ones. Available options are:
34 Produces a warning whenever an array is used in an implicit scalar
35 context. For example, both of the lines
39 will elicit a warning. Using an explicit B<scalar()> silences the
44 =item B<implicit-read> and B<implicit-write>
46 These options produce a warning whenever an operation implicitly
47 reads or (respectively) writes to one of Perl's special variables.
48 For example, B<implicit-read> will warn about these:
52 and B<implicit-write> will warn about these:
56 Both B<implicit-read> and B<implicit-write> warn about this:
62 This option warns whenever a bareword is implicitly quoted, but is also
63 the name of a subroutine in the current package. Typical mistakes that it will
66 use constant foo => 'bar';
70 Neither of these will do what a naive user would expect.
72 =item B<dollar-underscore>
74 This option warns whenever $_ is used either explicitly anywhere or
75 as the implicit argument of a B<print> statement.
77 =item B<private-names>
79 This option warns on each use of any variable, subroutine or
80 method name that lives in a non-current package but begins with
81 an underscore ("_"). Warnings aren't issued for the special case
82 of the single character name "_" by itself (e.g. $_ and @_).
84 =item B<undefined-subs>
86 This option warns whenever an undefined subroutine is invoked.
87 This option will only catch explicitly invoked subroutines such
88 as C<foo()> and not indirect invocations such as C<&$subref()>
89 or C<$obj-E<gt>meth()>. Note that some programs or modules delay
90 definition of subs until runtime by means of the AUTOLOAD
93 =item B<regexp-variables>
95 This option warns whenever one of the regexp variables $', $& or
96 $' is used. Any occurrence of any of these variables in your
97 program can slow your whole program down. See L<perlre> for
102 Turn all warnings on.
106 Turn all warnings off.
110 =head1 NON LINT-CHECK OPTIONS
116 Normally, Lint only checks the main code of the program together
117 with all subs defined in package main. The B<-u> option lets you
118 include other package names whose subs are then checked by Lint.
124 This is only a very preliminary version.
126 This module doesn't work correctly on thread-enabled perls.
130 Malcolm Beattie, mbeattie@sable.ox.ac.uk.
135 use B qw(walkoptree_slow main_root walksymtable svref_2object parents
136 OPf_WANT_LIST OPf_WANT OPf_STACKED G_ARRAY SVf_POK
139 my $file = "unknown"; # shadows current filename
140 my $line = 0; # shadows current line number
141 my $curstash = "main"; # shadows current stash
145 my %implies_ok_context;
147 map($implies_ok_context{$_}++,
148 qw(scalar av2arylen aelem aslice helem hslice
149 keys values hslice defined undef delete));
152 # Lint checks turned on by default
153 my @default_checks = qw(context);
158 map($valid_check{$_}++,
159 qw(context implicit_read implicit_write dollar_underscore
160 private_names bare_subs undefined_subs regexp_variables));
166 my %done_cv; # used to mark which subs have already been linted
167 my @extra_packages; # Lint checks mainline code and all subs which are
168 # in main:: or in one of these packages.
171 my $format = (@_ < 2) ? "%s" : shift;
172 warn sprintf("$format at %s line %d\n", @_, $file, $line);
175 # This gimme can't cope with context that's only determined
176 # at runtime via dowantarray().
179 my $flags = $op->flags;
180 if ($flags & OPf_WANT) {
181 return(($flags & OPf_WANT) == OPf_WANT_LIST ? 1 : 0);
190 if ($op->name eq "nextstate") {
193 $curstash = $op->stash->NAME;
199 my $opname = $op->name;
200 if ($check{context} && ($opname eq "rv2av" || $opname eq "rv2hv")) {
201 my $parent = parents->[0];
202 my $pname = $parent->name;
203 return if gimme($op) || $implies_ok_context{$pname};
204 # Two special cases to deal with: "foreach (@foo)" and "delete $a{$b}"
205 # null out the parent so we have to check for a parent of pp_null and
206 # a grandparent of pp_enteriter or pp_delete
207 if ($pname eq "null") {
208 my $gpname = parents->[1]->name;
209 return if $gpname eq "enteriter" || $gpname eq "delete";
211 warning("Implicit scalar context for %s in %s",
212 $opname eq "rv2av" ? "array" : "hash", $parent->desc);
214 if ($check{private_names} && $opname eq "method") {
215 my $methop = $op->first;
216 if ($methop->name eq "const") {
217 my $method = $methop->sv->PV;
218 if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
219 warning("Illegal reference to private method name $method");
227 if ($check{implicit_read}) {
228 if ($op->name eq "match" && !($op->flags & OPf_STACKED)) {
229 warning('Implicit match on $_');
232 if ($check{implicit_write}) {
233 if ($op->name eq "subst" && !($op->flags & OPf_STACKED)) {
234 warning('Implicit substitution on $_');
241 if ($check{implicit_read} || $check{implicit_write}) {
242 if ($op->name eq "enteriter") {
243 my $last = $op->last;
244 if ($last->name eq "gv" && $last->gv->NAME eq "_") {
245 warning('Implicit use of $_ in foreach');
253 if ( $check{bare_subs} && $op->name eq 'const'
254 && $op->private & 64 ) # OPpCONST_BARE = 64 in op.h
257 if( $sv->FLAGS & SVf_POK && exists &{$curstash.'::'.$sv->PV} ) {
258 warning "Bare sub name '" . $sv->PV . "' interpreted as string";
261 if ($check{dollar_underscore} && $op->name eq "gvsv"
262 && $op->gv->NAME eq "_")
264 warning('Use of $_');
266 if ($check{private_names}) {
267 my $opname = $op->name;
268 if ($opname eq "gv" || $opname eq "gvsv") {
270 if ($gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash) {
271 warning('Illegal reference to private name %s', $gv->NAME);
273 } elsif ($opname eq "method_named") {
274 my $method = $op->gv->PV;
275 if ($method =~ /^_./) {
276 warning("Illegal reference to private method name $method");
280 if ($check{undefined_subs}) {
281 if ($op->name eq "gv"
282 && $op->next->name eq "entersub")
285 my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
287 if (!defined(&$subname)) {
288 $subname =~ s/^main:://;
289 warning('Undefined subroutine %s called', $subname);
293 if ($check{regexp_variables} && $op->name eq "gvsv") {
294 my $name = $op->gv->NAME;
295 if ($name =~ /^[&'`]$/) {
296 warning('Use of regexp variable $%s', $name);
304 #warn sprintf("lintcv: %s::%s (done=%d)\n",
305 # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
306 return if !$$cv || $done_cv{$$cv}++;
307 my $root = $cv->ROOT;
308 #warn " root = $root (0x$$root)\n";#debug
309 walkoptree_slow($root, "lint") if $$root;
314 walkoptree_slow(main_root, "lint") if ${main_root()};
316 # Now do subs in main
317 no strict qw(vars refs);
319 for my $sym (keys %main::) {
320 next if $sym =~ /::$/;
321 *glob = $main::{$sym};
322 svref_2object(\*glob)->EGV->lintcv;
325 # Now do subs in non-main packages given by -u options
326 map { $search_pack{$_} = 1 } @extra_packages;
327 walksymtable(\%{"main::"}, "lintcv", sub {
330 #warn "Considering $package\n";#debug
331 return exists $search_pack{$package};
337 my ($option, $opt, $arg);
338 # Turn on default lint checks
339 for $opt (@default_checks) {
343 while ($option = shift @options) {
344 if ($option =~ /^-(.)(.*)/) {
348 unshift @options, $option;
351 if ($opt eq "-" && $arg eq "-") {
354 } elsif ($opt eq "D") {
355 $arg ||= shift @options;
356 foreach $arg (split(//, $arg)) {
359 } elsif ($arg eq "O") {
363 } elsif ($opt eq "u") {
364 $arg ||= shift @options;
365 push(@extra_packages, $arg);
368 foreach $opt (@default_checks, @options) {
371 %check = %valid_check;
373 elsif ($opt eq "none") {
377 if ($opt =~ s/^no_//) {
383 warn "No such check: $opt\n" unless defined $valid_check{$opt};
386 # Remaining arguments are things to check