perlcritic
perlcritic is a comprehensive static source code analysis engine for Perl that enforces coding standards and best practices. Based primarily on Damian Conway's "Perl Best Practices" book, it serves as an extensible framework for creating and applying consistent coding standards across Perl projects.
Key Features:
- Extensive Policy Library: Over 100 built-in policies covering bugs, security, maintainability, complexity, and code style
- Configurable Severity Levels: Five severity levels (1-5) allowing fine-tuned control over policy enforcement
- Best Practices Foundation: Policies based on "Perl Best Practices" and community-established coding standards
- Theme-Based Organization: Policies grouped by themes like bugs, security, performance, and maintainability
- Inline Annotations: Selective policy disabling through source code comments and annotations
- Extensible Framework: Support for custom policies and organizational coding standards
- Detailed Reporting: Comprehensive violation reports with explanations and suggested fixes
- Flexible Configuration: Project-specific configuration files for team-wide standards enforcement
perlcritic documentation
- Version in MegaLinter: 1.156
- Visit Official Web Site
- See How to configure perlcritic rules
- See How to disable perlcritic rules in files
- See Index of problems detected by perlcritic
Configuration in MegaLinter
- Enable perlcritic by adding
PERL_PERLCRITICin ENABLE_LINTERS variable - Disable perlcritic by adding
PERL_PERLCRITICin DISABLE_LINTERS variable
| Variable | Description | Default value |
|---|---|---|
| PERL_PERLCRITIC_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
| PERL_PERLCRITIC_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
| PERL_PERLCRITIC_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
| PERL_PERLCRITIC_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
| PERL_PERLCRITIC_CLI_LINT_MODE | Override default CLI lint mode - file: Calls the linter for each file- project: Call the linter from the root of the project |
file |
| PERL_PERLCRITIC_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".pl", ".pm", ".t", ""] |
| PERL_PERLCRITIC_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
| PERL_PERLCRITIC_PRE_COMMANDS | List of bash commands to run before the linter | None |
| PERL_PERLCRITIC_POST_COMMANDS | List of bash commands to run after the linter | None |
| PERL_PERLCRITIC_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling PERL_PERLCRITIC and its pre/post commands | None |
| PERL_PERLCRITIC_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
| PERL_PERLCRITIC_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
| PERL_PERLCRITIC_CLI_EXECUTABLE | Override CLI executable | ['perlcritic'] |
MegaLinter Flavors
This linter is available in the following flavors
| Flavor | Description | Embedded linters | Info | |
|---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 136 |
Behind the scenes
How are identified applicable files
- File extensions:
.pl,.pm,.t, `` - Detected file content (regex):
^#!/usr/bin/env perl,^#!/usr/bin/perl
How the linting is performed
- perlcritic is called one time by identified file (
fileCLI lint mode)
Example calls
perlcritic myfile.pl
Help content
Usage:
perlcritic [-12345 | --brutal | --cruel | --harsh | --stern | --gentle]
[--severity number | name] [{-p | --profile} file | --noprofile]
[--top [ number ]] [--theme expression] [--include pattern]
[--exclude pattern] [{-s | --single-policy} pattern]
[--only | --noonly] [--profile-strictness {warn|fatal|quiet}]
[--force | --noforce] [--statistics] [--statistics-only]
[--count | -C] [--verbose {number | format}] [--allow-unsafe]
[--color | --nocolor] [--pager pager] [--quiet]
[--color-severity-highest color_specification]
[--color-severity-high color_specification]
[--color-severity-medium color_specification]
[--color-severity-low color_specification]
[--color-severity-lowest color_specification]
[--files-with-violations | -l]
[--files-without-violations | -L]
[--program-extensions file_name_extension]
{FILE | DIRECTORY | STDIN}
perlcritic --profile-proto
perlcritic { --list | --list-enabled | --list-themes | --doc pattern [...] }
perlcritic { --help | --options | --man | --version }
Installation on mega-linter Docker image
- Dockerfile commands :
# renovate: datasource=github-tags depName=skaji/cpm
ARG PERL_PERLCRITIC_VERSION=v1.1.1
RUN curl -fsSL https://raw.githubusercontent.com/skaji/cpm/refs/tags/${PERL_PERLCRITIC_VERSION}/cpm | perl - install -g --show-build-log-on-failure --without-build --without-test --without-runtime Perl::Critic \
&& rm -rf /root/.perl-cpm
Known errors and resolutions
When this linter fails for a known non-lint reason (remote service unavailable, malformed config, missing credentials, etc.), MegaLinter detects the pattern below in the linter output and surfaces the matching guidance.
PERL_PERLCRITIC_ERROR_CONFIG_INVALID
Detection pattern (regex):
(Unable to parse profile|Invalid (option|setting) .* in profile|Perl::Critic::ConfigErrors)
Resolution guidance:
perlcritic could not parse its configuration file (`.perlcriticrc`, INI-like format).
Resolutions:
- Verify `.perlcriticrc` syntax: global settings at the top, then policy sections like `[Subroutines::ProhibitExplicitReturnUndef]`.
- Make sure every referenced policy is actually installed (try `perlcritic --list` to see available policies).
- Run with `perlcritic --profile /dev/null myfile.pl` to confirm the failure is config-related.
PERL_PERLCRITIC_ERROR_POLICY_NOT_FOUND
Detection pattern (regex):
Policy '[^']+' is not installed
Resolution guidance:
perlcritic's profile references a policy module that is not installed in the MegaLinter image.
Resolutions:
- Remove the unknown policy section from `.perlcriticrc`, or
- Pre-install the missing CPAN module via a pre-command in your .mega-linter.yml (cpanm installs globally):
PERL_PERLCRITIC_PRE_COMMANDS:
- command: "cpanm --notest Perl::Critic::Policy::YourPolicy"
cwd: "root"
continue_if_failed: false
