perl -MO=Lint[,OPTIONS] foo.pl
The B::Lint module is equivalent to an extended version of the B<-w>
option of B<perl>. It is named after the program F<lint> which carries
out a similar process for C programs.
=head1 OPTIONS AND LINT CHECKS
Option words are separated by commas (not whitespace) and follow the
usual conventions of compiler backend options. Following any options
(indicated by a leading B<->) come lint check arguments. Each such
argument (apart from the special B<all> and B<none> options) is a
word representing one possible lint check (turning on that check) or
is B<no-foo> (turning off that check). Before processing the check
arguments, a standard list of checks is turned on. Later options
override earlier ones. Available options are:
Produces a warning whenever an array is used in an implicit scalar
context. For example, both of the lines
will elicit a warning. Using an explicit B<scalar()> silences the
=item B<implicit-read> and B<implicit-write>
These options produce a warning whenever an operation implicitly
reads or (respectively) writes to one of Perl's special variables.
For example, B<implicit-read> will warn about these:
and B<implicit-write> will warn about these:
Both B<implicit-read> and B<implicit-write> warn about this:
This option warns whenever a bareword is implicitly quoted, but is also
the name of a subroutine in the current package. Typical mistakes that it will
use constant foo => 'bar';
Neither of these will do what a naive user would expect.
=item B<dollar-underscore>
This option warns whenever C<$_> is used either explicitly anywhere or
as the implicit argument of a B<print> statement.
This option warns on each use of any variable, subroutine or
method name that lives in a non-current package but begins with
an underscore ("_"). Warnings aren't issued for the special case
of the single character name "_" by itself (e.g. C<$_> and C<@_>).
This option warns whenever an undefined subroutine is invoked.
This option will only catch explicitly invoked subroutines such
as C<foo()> and not indirect invocations such as C<&$subref()>
or C<$obj-E<gt>meth()>. Note that some programs or modules delay
definition of subs until runtime by means of the AUTOLOAD
=item B<regexp-variables>
This option warns whenever one of the regexp variables C<$`>, C<$&> or C<$'>
is used. Any occurrence of any of these variables in your
program can slow your whole program down. See L<perlre> for
=head1 NON LINT-CHECK OPTIONS
Normally, Lint only checks the main code of the program together
with all subs defined in package main. The B<-u> option lets you
include other package names whose subs are then checked by Lint.
This is only a very preliminary version.
This module doesn't work correctly on thread-enabled perls.
Malcolm Beattie, mbeattie@sable.ox.ac.uk.
use B
qw(walkoptree_slow main_root walksymtable svref_2object parents
OPf_WANT_LIST OPf_WANT OPf_STACKED G_ARRAY SVf_POK
my $file = "unknown"; # shadows current filename
my $line = 0; # shadows current line number
my $curstash = "main"; # shadows current stash
map($implies_ok_context{$_}++,
qw(scalar av2arylen aelem aslice helem hslice
keys values hslice defined undef delete));
# Lint checks turned on by default
my @default_checks = qw(context);
qw(context implicit_read implicit_write dollar_underscore
private_names bare_subs undefined_subs regexp_variables));
my %done_cv; # used to mark which subs have already been linted
my @extra_packages; # Lint checks mainline code and all subs which are
# in main:: or in one of these packages.
my $format = (@_ < 2) ?
"%s" : shift;
warn sprintf("$format at %s line %d\n", @_, $file, $line);
# This gimme can't cope with context that's only determined
# at runtime via dowantarray().
return(($flags & OPf_WANT
) == OPf_WANT_LIST ?
1 : 0);
if ($op->name eq "nextstate") {
$curstash = $op->stash->NAME;
if ($check{context
} && ($opname eq "rv2av" || $opname eq "rv2hv")) {
my $parent = parents
->[0];
my $pname = $parent->name;
return if gimme
($op) || $implies_ok_context{$pname};
# Two special cases to deal with: "foreach (@foo)" and "delete $a{$b}"
# null out the parent so we have to check for a parent of pp_null and
# a grandparent of pp_enteriter or pp_delete
my $gpname = parents
->[1]->name;
return if $gpname eq "enteriter" || $gpname eq "delete";
warning
("Implicit scalar context for %s in %s",
$opname eq "rv2av" ?
"array" : "hash", $parent->desc);
if ($check{private_names
} && $opname eq "method") {
if ($methop->name eq "const") {
my $method = $methop->sv->PV;
if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
warning
("Illegal reference to private method name $method");
if ($check{implicit_read
}) {
if ($op->name eq "match" && !($op->flags & OPf_STACKED
)) {
warning
('Implicit match on $_');
if ($check{implicit_write
}) {
if ($op->name eq "subst" && !($op->flags & OPf_STACKED
)) {
warning
('Implicit substitution on $_');
if ($check{implicit_read
} || $check{implicit_write
}) {
if ($op->name eq "enteriter") {
if ($last->name eq "gv" && $last->gv->NAME eq "_") {
warning
('Implicit use of $_ in foreach');
if ( $check{bare_subs
} && $op->name eq 'const'
&& $op->private & 64 ) # OPpCONST_BARE = 64 in op.h
if( $sv->FLAGS & SVf_POK
&& exists &{$curstash.'::'.$sv->PV} ) {
warning
"Bare sub name '" . $sv->PV . "' interpreted as string";
if ($check{dollar_underscore
} && $op->name eq "gvsv"
if ($check{private_names
}) {
if ($opname eq "gv" || $opname eq "gvsv") {
if ($gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash) {
warning
('Illegal reference to private name %s', $gv->NAME);
} elsif ($opname eq "method_named") {
my $method = $op->gv->PV;
warning
("Illegal reference to private method name $method");
if ($check{undefined_subs
}) {
&& $op->next->name eq "entersub")
my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
if (!defined(&$subname)) {
warning
('Undefined subroutine %s called', $subname);
if ($check{regexp_variables
} && $op->name eq "gvsv") {
my $name = $op->gv->NAME;
if ($name =~ /^[&'`]$/) {
warning
('Use of regexp variable $%s', $name);
#warn sprintf("lintcv: %s::%s (done=%d)\n",
# $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
return if !$$cv || $done_cv{$$cv}++;
#warn " root = $root (0x$$root)\n";#debug
walkoptree_slow
($root, "lint") if $$root;
walkoptree_slow
(main_root
, "lint") if ${main_root
()};
for my $sym (keys %main::) {
svref_2object
(\
*glob)->EGV->lintcv;
# Now do subs in non-main packages given by -u options
map { $search_pack{$_} = 1 } @extra_packages;
walksymtable
(\
%{"main::"}, "lintcv", sub {
#warn "Considering $package\n";#debug
return exists $search_pack{$package};
my ($option, $opt, $arg);
# Turn on default lint checks
for $opt (@default_checks) {
while ($option = shift @options) {
if ($option =~ /^-(.)(.*)/) {
unshift @options, $option;
if ($opt eq "-" && $arg eq "-") {
foreach $arg (split(//, $arg)) {
push(@extra_packages, $arg);
foreach $opt (@default_checks, @options) {
warn "No such check: $opt\n" unless defined $valid_check{$opt};
# Remaining arguments are things to check