perl-lint

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Perl Linting and Formatting

Perl代码检查与格式化

Run Perl::Critic analysis and perltidy formatting with automatic tool detection.
自动检测工具,运行Perl::Critic分析和perltidy格式化。

Workflow

工作流

  1. Check if perlcritic/perltidy are installed
  2. Run analysis on specified files
  3. Report findings with severity levels
  4. Suggest fixes or format code
  1. 检查perlcritic/perltidy是否已安装
  2. 对指定文件运行分析
  3. 按严重级别报告发现的问题
  4. 提供修复建议或格式化代码

Tool Detection

工具检测

First, verify tools are available:
bash
undefined
首先验证工具是否可用:
bash
undefined

Check perlcritic

Check perlcritic

command -v perlcritic >/dev/null && echo "perlcritic available" || echo "Install: cpanm Perl::Critic"
command -v perlcritic >/dev/null && echo "perlcritic available" || echo "Install: cpanm Perl::Critic"

Check perltidy

Check perltidy

command -v perltidy >/dev/null && echo "perltidy available" || echo "Install: cpanm Perl::Tidy"
undefined
command -v perltidy >/dev/null && echo "perltidy available" || echo "Install: cpanm Perl::Tidy"
undefined

Perl::Critic Analysis

Perl::Critic分析

Running perlcritic

运行perlcritic

bash
undefined
bash
undefined

Default analysis (severity 5 - gentle)

Default analysis (severity 5 - gentle)

perlcritic script.pl
perlcritic script.pl

All severities (1 = strictest)

All severities (1 = strictest)

perlcritic --severity 1 script.pl
perlcritic --severity 1 script.pl

Specific severity level

Specific severity level

perlcritic --severity 3 script.pl
perlcritic --severity 3 script.pl

With verbose explanations

With verbose explanations

perlcritic --verbose 11 script.pl
perlcritic --verbose 11 script.pl

Single policy

Single policy

perlcritic --single-policy RequireUseStrict script.pl
perlcritic --single-policy RequireUseStrict script.pl

Exclude policies

Exclude policies

perlcritic --exclude RequireUseWarnings script.pl
undefined
perlcritic --exclude RequireUseWarnings script.pl
undefined

Severity Levels

严重级别

LevelNameDescription
5GentleObvious, unambiguous issues
4SternUsually bad practices
3HarshControversial but recommended
2CruelPickier about style
1BrutalMaximum strictness
级别名称描述
5温和明显、无歧义的问题
4严格通常属于不良实践
3苛刻存在争议但仍推荐遵循
2严苛对代码风格要求更高
1最高严格最大严格度

Common Policies

常用规则

PolicySeverityIssue
RequireUseStrict5Missing
use strict
RequireUseWarnings4Missing
use warnings
ProhibitBarewordFileHandles5Using bareword filehandles
ProhibitTwoArgOpen5Two-argument open()
ProhibitStringyEval5Using eval with string
RequireTidyCode1Code not formatted
规则名称严重级别问题
RequireUseStrict5缺少
use strict
声明
RequireUseWarnings4缺少
use warnings
声明
ProhibitBarewordFileHandles5使用了裸字文件句柄
ProhibitTwoArgOpen5使用了双参数形式的open()
ProhibitStringyEval5对字符串使用eval
RequireTidyCode1代码未格式化

Configuration File

配置文件

Create
.perlcriticrc
in project root:
ini
undefined
在项目根目录创建
.perlcriticrc
ini
undefined

.perlcriticrc

.perlcriticrc

severity = 3 verbose = 8 theme = core
severity = 3 verbose = 8 theme = core

Exclude specific policies

Exclude specific policies

[-Documentation::RequirePodSections] [-InputOutput::RequireBriefOpen]
[-Documentation::RequirePodSections] [-InputOutput::RequireBriefOpen]

Configure specific policy

Configure specific policy

[CodeLayout::RequireTidyCode] perltidyrc = .perltidyrc
[Variables::ProhibitPackageVars] packages = Data::Dumper File::Find
[TestingAndDebugging::ProhibitNoStrict] allow = refs
undefined
[CodeLayout::RequireTidyCode] perltidyrc = .perltidyrc
[Variables::ProhibitPackageVars] packages = Data::Dumper File::Find
[TestingAndDebugging::ProhibitNoStrict] allow = refs
undefined

Verbose Formats

详细输出格式

LevelOutput Format
1Line only
4Line + Column
8Line + Policy + Severity
10Full explanation
11With PBP page reference
级别输出格式
1仅显示行号
4行号 + 列号
8行号 + 规则名称 + 严重级别
10完整解释
11附带PBP页面引用

Perltidy Formatting

Perltidy格式化

Basic Usage

基础用法

bash
undefined
bash
undefined

Format file (creates .bak backup)

Format file (creates .bak backup)

perltidy script.pl
perltidy script.pl

Format to stdout (no backup)

Format to stdout (no backup)

perltidy -st script.pl
perltidy -st script.pl

In-place edit (no backup)

In-place edit (no backup)

perltidy -b -nst script.pl
perltidy -b -nst script.pl

Check if tidy (exit code)

Check if tidy (exit code)

perltidy -st script.pl | diff -q - script.pl
undefined
perltidy -st script.pl | diff -q - script.pl
undefined

Common Options

常用选项

bash
undefined
bash
undefined

Basic formatting

Basic formatting

perltidy -i=4 -ci=4 -l=100 script.pl
perltidy -i=4 -ci=4 -l=100 script.pl

Full command with common options

Full command with common options

perltidy
--indent-columns=4
--continuation-indentation=4
--maximum-line-length=100
--vertical-tightness=2
--paren-tightness=1
--brace-tightness=1
script.pl
undefined
perltidy
--indent-columns=4
--continuation-indentation=4
--maximum-line-length=100
--vertical-tightness=2
--paren-tightness=1
--brace-tightness=1
script.pl
undefined

Configuration File

配置文件

Create
.perltidyrc
in project root:
text
undefined
在项目根目录创建
.perltidyrc
text
undefined

.perltidyrc

.perltidyrc

Indentation

Indentation

--indent-columns=4 --continuation-indentation=4
--indent-columns=4 --continuation-indentation=4

Line length

Line length

--maximum-line-length=100
--maximum-line-length=100

Blank lines

Blank lines

--blank-lines-before-subs=1 --blank-lines-after-block-opening=0
--blank-lines-before-subs=1 --blank-lines-after-block-opening=0

Spaces

Spaces

--paren-tightness=1 --brace-tightness=1 --square-bracket-tightness=1
--paren-tightness=1 --brace-tightness=1 --square-bracket-tightness=1

Vertical alignment

Vertical alignment

--no-outdent-long-quotes --no-outdent-long-comments
--no-outdent-long-quotes --no-outdent-long-comments

Output

Output

--backup-and-modify-in-place --backup-file-extension=/
undefined
--backup-and-modify-in-place --backup-file-extension=/
undefined

Combined Workflow

组合工作流

Lint and Fix

检查并修复

bash
#!/bin/bash
bash
#!/bin/bash

lint-perl.sh

lint-perl.sh

FILE="$1"
if [[ ! -f "$FILE" ]]; then echo "Usage: lint-perl.sh <file.pl>" exit 1 fi
echo "=== Syntax Check ===" perl -c "$FILE" || exit 1
echo "" echo "=== Perl::Critic ===" if command -v perlcritic >/dev/null; then perlcritic --severity 4 "$FILE" else echo "perlcritic not installed. Run: cpanm Perl::Critic" fi
echo "" echo "=== Formatting Check ===" if command -v perltidy >/dev/null; then if ! perltidy -st "$FILE" | diff -q - "$FILE" >/dev/null 2>&1; then echo "File needs formatting. Run: perltidy $FILE" else echo "File is properly formatted." fi else echo "perltidy not installed. Run: cpanm Perl::Tidy" fi
undefined
FILE="$1"
if [[ ! -f "$FILE" ]]; then echo "Usage: lint-perl.sh <file.pl>" exit 1 fi
echo "=== Syntax Check ===" perl -c "$FILE" || exit 1
echo "" echo "=== Perl::Critic ===" if command -v perlcritic >/dev/null; then perlcritic --severity 4 "$FILE" else echo "perlcritic not installed. Run: cpanm Perl::Critic" fi
echo "" echo "=== Formatting Check ===" if command -v perltidy >/dev/null; then if ! perltidy -st "$FILE" | diff -q - "$FILE" >/dev/null 2>&1; then echo "File needs formatting. Run: perltidy $FILE" else echo "File is properly formatted." fi else echo "perltidy not installed. Run: cpanm Perl::Tidy" fi
undefined

Interpreting Results

结果解读

Severity 5 (Critical)

严重级别5(临界)

Must fix immediately:
text
Bareword file handle opened at line 10, column 1.
Two-argument "open" used at line 15, column 5.
Fix:
perl
undefined
必须立即修复:
text
Bareword file handle opened at line 10, column 1.
Two-argument "open" used at line 15, column 5.
修复方案:
perl
undefined

Wrong

Wrong

open FILE, $filename;
open FILE, $filename;

Correct

Correct

open my $fh, '<', $filename;
undefined
open my $fh, '<', $filename;
undefined

Severity 4 (Important)

严重级别4(重要)

Should fix:
text
Code before strictures are enabled at line 1.
Fix:
perl
undefined
应该修复:
text
Code before strictures are enabled at line 1.
修复方案:
perl
undefined

Add at top

Add at top

use strict; use warnings;
undefined
use strict; use warnings;
undefined

Severity 3 (Recommended)

严重级别3(推荐)

Consider fixing:
text
Regular expression without "/x" flag at line 25.
建议考虑修复:
text
Regular expression without "/x" flag at line 25.

Installation

安装

If tools are missing:
bash
undefined
如果缺少工具:
bash
undefined

Install both tools

Install both tools

cpanm Perl::Critic Perl::Tidy
cpanm Perl::Critic Perl::Tidy

Verify installation

Verify installation

perlcritic --version perltidy --version
undefined
perlcritic --version perltidy --version
undefined

Quick Commands

快捷命令

Lint specific file:
bash
perlcritic --severity 4 --verbose 8 path/to/file.pl
Lint all Perl files in directory:
bash
find . -name '*.pl' -o -name '*.pm' | xargs perlcritic --severity 4
Format all files:
bash
find . -name '*.pl' -o -name '*.pm' | xargs perltidy -b
检查指定文件:
bash
perlcritic --severity 4 --verbose 8 path/to/file.pl
检查目录下所有Perl文件:
bash
find . -name '*.pl' -o -name '*.pm' | xargs perlcritic --severity 4
格式化所有文件:
bash
find . -name '*.pl' -o -name '*.pm' | xargs perltidy -b