DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j
  • Avoiding AI Debt as a Team
  • Why Successful Tech Transformation Starts With Clarity
  • Beyond State Consistency: Preventing Stale Decisions in AI Agent Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Everyone's a (Perl) Critic, and You Can Be Too!

Everyone's a (Perl) Critic, and You Can Be Too!

Learn the basics of perlcritic, a tool for code readability, security, and bug prevention.

By 
Mark Gardner
DZone Core CORE ·
Sep. 21, 21 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
2.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Everyone's a (Perl) Critic, and You Can Be Too!

The perlcritic tool is often your first defense against "awkward, hard to read, error-prone, or unconventional constructs in your code," per its description. It's part of a class of programs historically known as linters, so-called because like a clothes dryer machine's lint trap, they "detect small errors with big effects." (Another such linter is perltidy, which I've referenced in the past.)

You can use perlcritic at the command line, integrated with your editor, as a git pre-commit hook, or (my preference) as part of your author tests. It's driven by policies, individual modules that check your code against a particular recommendation, many of them from Damian Conway's Perl Best Practices (2005). Those policies, in turn, are enabled by PPI, a library that transforms Perl code into documents that can be programmatically examined and manipulated much like the Document Object Model (DOM) is used to programmatically access web pages.

perlcritic enables the following policies by default unless you customize its configuration or install more. These are just the "gentle" (severity level 5) policies, so consider them the bare minimum in detecting bad practices. The full set of included policies goes much deeper, ratcheting up the severity to "stern," "harsh," "cruel," and "brutal." They're further organized according to themes so that you might selectively review your code against issues like security, maintenance, complexity, and bug prevention.

  • BuiltinFunctions::ProhibitSleepViaSelect
  • BuiltinFunctions::ProhibitStringyEval
  • BuiltinFunctions::RequireGlobFunction
  • ClassHierarchies::ProhibitOneArgBless
  • ControlStructures::ProhibitMutatingListFunctions
  • InputOutput::ProhibitBarewordFileHandles
  • InputOutput::ProhibitInteractiveTest
  • InputOutput::ProhibitTwoArgOpen
  • InputOutput::RequireEncodingWithUTF8Layer
  • Modules::ProhibitEvilModules
  • Modules::RequireBarewordIncludes
  • Modules::RequireFilenameMatchesPackage
  • Subroutines::ProhibitExplicitReturnUndef
  • Subroutines::ProhibitNestedSubs
  • Subroutines::ProhibitReturnSort
  • Subroutines::ProhibitSubroutinePrototypes
  • TestingAndDebugging::ProhibitNoStrict
  • TestingAndDebugging::RequireUseStrict
  • ValuesAndExpressions::ProhibitLeadingZeros
  • Variables::ProhibitConditionalDeclarations
  • Variables::RequireLexicalLoopIterators

My favorite above is probably ProhibitEvilModules. Aside from the colorful name, a development team can use it to steer people towards an organization's favored solutions rather than "deprecated, buggy, unsupported, or insecure" ones. By default, it prohibits Class::ISA, Pod::Plainer, Shell, and Switch, but you should curate and configure a list within your team.

Speaking of working within a team, although perlcritic is meant to be a vital tool to ensure good practices, it's no substitute for manual peer code review. Those reviews can lead to the creation or adoption of new automated policies to save time and settle arguments, but such work should be done collaboratively after achieving some kind of consensus. This is true whether you're a team of employees working on proprietary software or a group of volunteers developing open source.

Of course, reasonable people can and do disagree over any of the included policies, but as a reasonable person, you should have good reasons to disagree before you either configure perlcritic appropriately or selectively and knowingly bend the rules where required. Other CPAN authors have even provided their own additions to perlcritic, so it's worth searching CPAN under "Perl::Critic::Policy::" for more examples. In particular, these community-inspired policies group a number of recommendations from Perl developers on Internet Relay Chat (IRC).

Personally, although I adhere to my employer's standardized configuration when testing and reviewing code, I like to run perlcritic on the "brutal" setting before committing my own. What do you prefer? Let me know in the comments below.


Cover image: "Everyone's a critic - graifitti under Mancunian Way in Manchester" by Alex Pepperhill is licensed under CC BY-ND 2.0.

Perl (programming language)

Published at DZone with permission of Mark Gardner. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook