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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Accelerating AI Inference With TensorRT
  • Why Documentation Matters More Than You Think
  • Zero Trust for AWS NLBs: Why It Matters and How to Do 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 user avatar
Mark Gardner
DZone Core CORE ·
Sep. 21, 21 · Analysis
Likes (4)
Comment
Save
Tweet
Share
2.5K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!