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 F<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
40 will elicit a warning. Using an explicit B<scalar()> silences the
45 =item B<implicit-read> and B<implicit-write>
47 These options produce a warning whenever an operation implicitly
48 reads or (respectively) writes to one of Perl's special variables.
49 For example, B<implicit-read> will warn about these:
53 and B<implicit-write> will warn about these:
57 Both B<implicit-read> and B<implicit-write> warn about this:
63 This option warns whenever a bareword is implicitly quoted, but is also
64 the name of a subroutine in the current package. Typical mistakes that it will
67 use constant foo => 'bar';
71 Neither of these will do what a naive user would expect.
73 =item B<dollar-underscore>
75 This option warns whenever C<$_> is used either explicitly anywhere or
76 as the implicit argument of a B<print> statement.
78 =item B<private-names>
80 This option warns on each use of any variable, subroutine or
81 method name that lives in a non-current package but begins with
82 an underscore ("_"). Warnings aren't issued for the special case
83 of the single character name "_" by itself (e.g. C<$_> and C<@_>).
85 =item B<undefined-subs>
87 This option warns whenever an undefined subroutine is invoked.
88 This option will only catch explicitly invoked subroutines such
89 as C<foo()> and not indirect invocations such as C<&$subref()>
90 or C<$obj-E<gt>meth()>. Note that some programs or modules delay
91 definition of subs until runtime by means of the AUTOLOAD
94 =item B<regexp-variables>
96 This option warns whenever one of the regexp variables C<$`>, C<$&> or C<$'>
97 is used. Any occurrence of any of these variables in your
98 program can slow your whole program down. See L<perlre> for
103 Turn all warnings on.
107 Turn all warnings off.
111 =head1 NON LINT-CHECK OPTIONS
117 Normally, Lint only checks the main code of the program together
118 with all subs defined in package main. The B<-u> option lets you
119 include other package names whose subs are then checked by Lint.
123 =head1 EXTENDING LINT
125 Lint can be extended by registering plugins.
127 The C<< B::Lint->register_plugin( MyPlugin => \@new_checks ) >> method
128 adds the class C<MyPlugin> to the list of plugins. It also adds the
129 list of C<@new_checks> to the list of valid checks.
131 You must create a C<match( \%checks )> method in your plugin class or one
132 of its parents. It will be called on every op as a regular method call
133 with a hash ref of checks as its parameter.
135 You may not alter the %checks hash reference.
137 The class methods C<< B::Lint->file >> and C<< B::Lint->line >> contain
138 the current filename and line number.
142 B::Lint->register_plugin( Sample => [ 'good_taste' ] );
145 my ( $op, $checks_href ) = shift;
146 if ( $checks_href->{good_taste} ) {
153 This is only a very preliminary version.
155 This module doesn't work correctly on thread-enabled perls.
159 Malcolm Beattie, mbeattie@sable.ox.ac.uk.
164 use B qw(walkoptree_slow main_root walksymtable svref_2object parents
167 OPf_WANT_VOID OPf_WANT_LIST OPf_WANT OPf_STACKED G_ARRAY SVf_POK
170 my $file = "unknown"; # shadows current filename
171 my $line = 0; # shadows current line number
172 my $curstash = "main"; # shadows current stash
179 my %implies_ok_context;
181 map($implies_ok_context{$_}++,
182 qw(scalar av2arylen aelem aslice helem hslice
183 keys values hslice defined undef delete));
186 # Lint checks turned on by default
187 my @default_checks = qw(context);
190 my %plugin_valid_check;
193 map($valid_check{$_}++,
194 qw(context implicit_read implicit_write dollar_underscore
195 private_names bare_subs undefined_subs regexp_variables));
201 my %done_cv; # used to mark which subs have already been linted
202 my @extra_packages; # Lint checks mainline code and all subs which are
203 # in main:: or in one of these packages.
206 my $format = (@_ < 2) ? "%s" : shift;
207 warn sprintf("$format at %s line %d\n", @_, $file, $line);
210 # This gimme can't cope with context that's only determined
211 # at runtime via dowantarray().
214 my $flags = $op->flags;
215 if ($flags & OPf_WANT) {
216 return(($flags & OPf_WANT) == OPf_WANT_LIST ? 1 : 0);
226 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
231 for \ ( *B::PADOP::lint,
238 if ($op->name eq "nextstate") {
241 $curstash = $op->stash->NAME;
245 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
251 my $opname = $op->name;
252 if ($check{context} && ($opname eq "rv2av" || $opname eq "rv2hv")) {
253 my $parent = parents->[0];
254 my $pname = $parent->name;
255 return if gimme($op) || $implies_ok_context{$pname};
256 # Three special cases to deal with: "foreach (@foo)", "delete $a{$b}", and "exists $a{$b}"
257 # null out the parent so we have to check for a parent of pp_null and
258 # a grandparent of pp_enteriter, pp_delete, pp_exists
259 if ($pname eq "null") {
260 my $gpname = parents->[1]->name;
261 return if $gpname eq "enteriter"
262 or $gpname eq "delete"
263 or $gpname eq "exists";
267 return if $op->private & OPpOUR_INTRO
268 and ( $op->flags & OPf_WANT ) == OPf_WANT_VOID;
270 warning("Implicit scalar context for %s in %s",
271 $opname eq "rv2av" ? "array" : "hash", $parent->desc);
273 if ($check{private_names} && $opname eq "method") {
274 my $methop = $op->first;
275 if ($methop->name eq "const") {
276 my $method = $methop->sv->PV;
277 if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
278 warning("Illegal reference to private method name $method");
284 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
290 if ($check{implicit_read}) {
291 if ($op->name eq "match"
292 and not ( $op->flags & OPf_STACKED
296 =~ /^(?:leave )?(?:null )*grep/ ) ) {
297 warning('Implicit match on $_');
300 if ($check{implicit_write}) {
301 if ($op->name eq "subst" && !($op->flags & OPf_STACKED)) {
302 warning('Implicit substitution on $_');
307 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
313 if ($check{implicit_read} || $check{implicit_write}) {
314 if ($op->name eq "enteriter") {
315 my $last = $op->last;
316 my $body = $op->redoop;
317 if ( $last->name eq "gv"
318 and $last->gv->NAME eq "_"
319 and $body->name =~ /\A(?:next|db|set)state\z/ ) {
320 warning('Implicit use of $_ in foreach');
326 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
330 sub _inside_foreach_statement {
331 for my $op ( @{ parents() || [] } ) {
332 $op->name eq 'leaveloop' or next;
333 my $first = $op->first;
334 $first->name eq 'enteriter' or next;
335 $first->redoop->name !~ /\A(?:next|db|set)state\z/ or next;
343 if ( $check{bare_subs} && $op->name eq 'const'
344 && $op->private & 64 ) # OPpCONST_BARE = 64 in op.h
347 if( $sv->FLAGS & SVf_POK && exists &{$curstash.'::'.$sv->PV} ) {
348 warning "Bare sub name '" . $sv->PV . "' interpreted as string";
351 if ($check{dollar_underscore}
352 and $op->name eq "gvsv"
353 and $op->gv->NAME eq "_"
354 and not ( _inside_foreach_statement()
355 or do { my $ctx = join( ' ',
357 @{ parents() || [] } );
358 $ctx =~ /(grep|map)start \1while/ } ) )
360 warning('Use of $_');
362 if ($check{private_names}) {
363 my $opname = $op->name;
364 if ($opname eq "gv" || $opname eq "gvsv") {
366 if ($gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash) {
367 warning('Illegal reference to private name %s', $gv->NAME);
369 } elsif ($opname eq "method_named") {
370 my $method = $op->gv->PV;
371 if ($method =~ /^_./) {
372 warning("Illegal reference to private method name $method");
376 if ($check{undefined_subs}) {
377 if ($op->name eq "gv"
378 && $op->next->name eq "entersub")
381 my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
383 if (!defined(&$subname)) {
384 $subname =~ s/^main:://;
385 warning('Undefined subroutine %s called', $subname);
389 if ($check{regexp_variables} && $op->name eq "gvsv") {
390 my $name = $op->gv->NAME;
391 if ($name =~ /^[&'`]$/) {
392 warning('Use of regexp variable $%s', $name);
397 $m = $_->can('match'), $op->$m( \ %check ) for @plugins;
404 #warn sprintf("lintcv: %s::%s (done=%d)\n",
405 # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
406 return if !$$cv || $done_cv{$$cv}++;
407 my $root = $cv->ROOT;
408 #warn " root = $root (0x$$root)\n";#debug
409 walkoptree_slow($root, "lint") if $$root;
414 walkoptree_slow(main_root, "lint") if ${main_root()};
416 # Now do subs in main
417 no strict qw(vars refs);
419 for my $sym (keys %main::) {
420 next if $sym =~ /::$/;
421 *glob = $main::{$sym};
423 # When is EGV a special value?
424 my $gv = svref_2object(\*glob)->EGV;
425 next if class( $gv ) eq 'SPECIAL';
429 # Now do subs in non-main packages given by -u options
430 map { $search_pack{$_} = 1 } @extra_packages;
431 walksymtable(\%{"main::"}, "lintcv", sub {
434 #warn "Considering $package\n";#debug
435 return exists $search_pack{$package};
441 my ($option, $opt, $arg);
442 # Turn on default lint checks
443 for $opt (@default_checks) {
447 while ($option = shift @options) {
448 if ($option =~ /^-(.)(.*)/) {
452 unshift @options, $option;
455 if ($opt eq "-" && $arg eq "-") {
458 } elsif ($opt eq "D") {
459 $arg ||= shift @options;
460 foreach $arg (split(//, $arg)) {
463 } elsif ($arg eq "O") {
467 } elsif ($opt eq "u") {
468 $arg ||= shift @options;
469 push(@extra_packages, $arg);
472 foreach $opt (@default_checks, @options) {
475 %check = ( %valid_check, %plugin_valid_check );
477 elsif ($opt eq "none") {
481 if ($opt =~ s/^no_//) {
487 warn "No such check: $opt\n" unless defined $valid_check{$opt}
488 or defined $plugin_valid_check{$opt};
491 # Remaining arguments are things to check
496 sub register_plugin {
497 my ( undef, $plugin, $new_checks ) = @_;
499 # Register the plugin
500 for my $check ( @$new_checks ) {
502 or warn "Undefined value in checks.";
503 not $valid_check{ $check }
504 or warn "$check is already registered as a B::Lint feature.";
505 not $plugin_valid_check{ $check }
506 or warn "$check is already registered as a $plugin_valid_check{$check} feature.";
508 $plugin_valid_check{$check} = $plugin;
511 push @plugins, $plugin;