Lint-like program for Perl?
Asked Answered
I

4

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.

Ier answered 26/7, 2011 at 19:10 Comment(2)
this is the first result for "perl lint" today.Alternation
Consider use warnings FATAL => 'all'; instead of just use warnings;.Counterstroke
B
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.

Biocatalyst answered 26/7, 2011 at 20:29 Comment(4)
Yes, Pel::Critic is not what I want for the reasons you describe. Thanks for the suggestion.Ier
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.Paralyze
@Volomike: Don't include the square brackets on the command-line. They were used to show that the stuff in them is optional.Biocatalyst
Be careful guys. -c can still execute your program and break you in hard way: perldoc.perl.org/…Kumar
K
20

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

Ketose answered 26/7, 2011 at 19:14 Comment(2)
@Ier 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.Ketose
You can also use Devel::Cover or other code profilers to find unreachable (untested in the case of Devel::Cover) code.Departmentalize
B
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.

Biocatalyst answered 26/7, 2011 at 20:29 Comment(4)
Yes, Pel::Critic is not what I want for the reasons you describe. Thanks for the suggestion.Ier
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.Paralyze
@Volomike: Don't include the square brackets on the command-line. They were used to show that the stuff in them is optional.Biocatalyst
Be careful guys. -c can still execute your program and break you in hard way: perldoc.perl.org/…Kumar
B
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
Brooklime answered 9/8, 2012 at 8:7 Comment(2)
B::Lint is deprecated at 5.18Preparative
@Preparative ...any references?Brooklime
N
4

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

Nicias answered 25/11, 2014 at 18:3 Comment(1)
// , Eeeeeveryone's a criticEolande

© 2022 - 2024 — McMap. All rights reserved.