How Perl::Critic Facilitates
Code Best Practices

Chris Dolan

MediaLandscape Software

cdolan@cpan.org

What is Perl::Critic?

Static source code analyzer

by Jeffrey Ryan Thalhammer (THALJEF)

Motivation

→ Use code to optimize for you!

Policies

Usage

% perlcritic PerfectCode.pm
# OK

% perlcritic LegacyCode.pm
Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5)
Expression form of 'eval' at line 4, column 4. See page 161 of PBP. (Severity: 5)

Usage

Lenient (the default) % perlcritic -5 Foo.pm

Typical % perlcritic -3 Foo.pm

Pedantic % perlcritic -1 Foo.pm

Examples: Modules::RequireBarewordIncludes ClassHierarchies::ProhibitExplicitISA CodeLayout::ProhibitParensWithBuiltins

Configuration

Create a ~/.perlcriticrc file

[-CodeLayout::RequireTidyCode]
[-ControlStructures::ProhibitCStyleForLoops]
[-Documentation::RequirePodAtEnd]
[-Documentation::RequirePodSections]
[-Miscellanea::RequireRcsKeywords]
[-NamingConventions::ProhibitMixedCaseSubs]

[ControlStructures::ProhibitCascadingIfElse]
max_elsif = 3

Making Exceptions

Perl::Critic has flags to ignore special cases

Learn more

And introducing P::C testing via the web: http://perlcritic.com