24

I'm looking for a lint for Perl, something that would catch dead code and other potential problems. Any suggestions?

I have

use strict;
use warnings;

already but I'd like to have more.

Charles
  • 11,269
  • 13
  • 67
  • 105

4 Answers4

20

Perl::Critic is your friend. I use Test::Perl::Critic and build it into my code's author tests

jrockway
  • 42,082
  • 9
  • 61
  • 86
Stuart Watt
  • 5,242
  • 2
  • 24
  • 31
  • 3
    @Charles yes, there's a `ControlStructures::ProhibitUnreachableCode` policy for that, but it isn't all knowing. I don't believe it analyzes all constant expressions, for example, but it has picked up most blocks of dead code in some of my work. – Stuart Watt Jul 26 '11 at 19:24
  • 9
    You can also use `Devel::Cover` or other code profilers to find unreachable (untested in the case of Devel::Cover) code. – Eric Strom Jul 26 '11 at 19:57
15

Perl doesn't have a direct equivalent to lint. A large part of the reason for that is that Perl doesn't provide quite as many ways to hang yourself as C does. The basic version of "lint" for Perl is this:

perl -Mstrict [-Mdiagnostics] -cw <file>

This causes perl to compile (but not run) the specified file with strictures and warnings turned on. You can use diagnostics if you want more verbose messages or leave it out if the terse ones are enough for you.

If you want something more try using Perl::Critic, but be aware that this isn't really lint, either. lint primarily concerns itself with errors (e.g. things that would prevent compilation, trigger runtime errors, be non-portable, rely on undefined behavior, etc.). Perl::Critic is more focused on enforcement of coding standards. While there is some overlap they're very different things.

Michael Carman
  • 30,628
  • 10
  • 74
  • 122
  • Yes, Pel::Critic is not what I want for the reasons you describe. Thanks for the suggestion. – Charles Aug 09 '11 at 17:09
  • On a Mac (El Capitan version), it gave me a directory error with [-Mdiagnostics]. However, I was able to simply run `perl -Mstrict -cw myscript.pl` and it syntax checked just fine. – Volomike Dec 02 '15 at 00:39
  • 3
    @Volomike: Don't include the square brackets on the command-line. They were used to show that the stuff in them is optional. – Michael Carman Dec 02 '15 at 15:15
  • 1
    Be careful guys. `-c` can still execute your program and break you in hard way: https://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END – Vitaly Dyatlov May 24 '18 at 15:04
5

Use B::Lint. You can use it on command line by calling O module with Lint as argument, e.g.:

you@there:~/sandbox$ perl -MO=Lint Some.pm 
Implicit scalar context for array in logical and (&&) at Some.pm line 121
Implicit scalar context for array in conditional expression at Some.pm line 49
Implicit scalar context for array in logical and (&&) at Some.pm line 132
Some.pm syntax OK
Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
4

In addition to Perl::Critic you might want to look at the newer Perl::Lint.

Tim Bunce
  • 1,082
  • 7
  • 14