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_slow 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(pp_scalar pp_av2arylen pp_aelem pp_aslice pp_helem pp_hslice
133 pp_keys pp_values pp_hslice pp_defined pp_undef pp_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->ppaddr eq "pp_nextstate") {
175 $file = $op->filegv->SV->PV;
177 $curstash = $op->stash->NAME;
183 my $ppaddr = $op->ppaddr;
184 if ($check{context} && ($ppaddr eq "pp_rv2av" || $ppaddr eq "pp_rv2hv")) {
185 my $parent = parents->[0];
186 my $pname = $parent->ppaddr;
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 "pp_null") {
192 my $gpname = parents->[1]->ppaddr;
193 return if $gpname eq "pp_enteriter" || $gpname eq "pp_delete";
195 warning("Implicit scalar context for %s in %s",
196 $ppaddr eq "pp_rv2av" ? "array" : "hash", $parent->desc);
198 if ($check{private_names} && $ppaddr eq "pp_method") {
199 my $methop = $op->first;
200 if ($methop->ppaddr eq "pp_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 my $ppaddr = $op->ppaddr;
213 if ($ppaddr eq "pp_match" && !($op->flags & OPf_STACKED)) {
214 warning('Implicit match on $_');
217 if ($check{implicit_write}) {
218 my $ppaddr = $op->ppaddr;
219 if ($ppaddr eq "pp_subst" && !($op->flags & OPf_STACKED)) {
220 warning('Implicit substitution on $_');
227 if ($check{implicit_read} || $check{implicit_write}) {
228 my $ppaddr = $op->ppaddr;
229 if ($ppaddr eq "pp_enteriter") {
230 my $last = $op->last;
231 if ($last->ppaddr eq "pp_gv" && $last->gv->NAME eq "_") {
232 warning('Implicit use of $_ in foreach');
240 if ($check{dollar_underscore} && $op->ppaddr eq "pp_gvsv"
241 && $op->gv->NAME eq "_")
243 warning('Use of $_');
245 if ($check{private_names}) {
246 my $ppaddr = $op->ppaddr;
248 if (($ppaddr eq "pp_gv" || $ppaddr eq "pp_gvsv")
249 && $gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash)
251 warning('Illegal reference to private name %s', $gv->NAME);
254 if ($check{undefined_subs}) {
255 if ($op->ppaddr eq "pp_gv" && $op->next->ppaddr eq "pp_entersub") {
257 my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
259 if (!defined(&$subname)) {
260 $subname =~ s/^main:://;
261 warning('Undefined subroutine %s called', $subname);
265 if ($check{regexp_variables} && $op->ppaddr eq "pp_gvsv") {
266 my $name = $op->gv->NAME;
267 if ($name =~ /^[&'`]$/) {
268 warning('Use of regexp variable $%s', $name);
276 #warn sprintf("lintcv: %s::%s (done=%d)\n",
277 # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
278 return if !$$cv || $done_cv{$$cv}++;
279 my $root = $cv->ROOT;
280 #warn " root = $root (0x$$root)\n";#debug
281 walkoptree_slow($root, "lint") if $$root;
286 walkoptree_slow(main_root, "lint") if ${main_root()};
288 # Now do subs in main
289 no strict qw(vars refs);
292 while (($sym, *glob) = each %{"main::"}) {
293 #warn "Trying $sym\n";#debug
294 svref_2object(\*glob)->EGV->lintcv unless $sym =~ /::$/;
297 # Now do subs in non-main packages given by -u options
298 map { $search_pack{$_} = 1 } @extra_packages;
299 walksymtable(\%{"main::"}, "lintcv", sub {
302 #warn "Considering $package\n";#debug
303 return exists $search_pack{$package};
309 my ($option, $opt, $arg);
310 # Turn on default lint checks
311 for $opt (@default_checks) {
315 while ($option = shift @options) {
316 if ($option =~ /^-(.)(.*)/) {
320 unshift @options, $option;
323 if ($opt eq "-" && $arg eq "-") {
326 } elsif ($opt eq "D") {
327 $arg ||= shift @options;
328 foreach $arg (split(//, $arg)) {
331 } elsif ($arg eq "O") {
335 } elsif ($opt eq "u") {
336 $arg ||= shift @options;
337 push(@extra_packages, $arg);
340 foreach $opt (@default_checks, @options) {
343 %check = %valid_check;
345 elsif ($opt eq "none") {
349 if ($opt =~ s/^no-//) {
355 warn "No such check: $opt\n" unless defined $valid_check{$opt};
358 # Remaining arguments are things to check