perl-lint
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePerl Linting and Formatting
Perl代码检查与格式化
Run Perl::Critic analysis and perltidy formatting with automatic tool detection.
自动检测工具,运行Perl::Critic分析和perltidy格式化。
Workflow
工作流
- Check if perlcritic/perltidy are installed
- Run analysis on specified files
- Report findings with severity levels
- Suggest fixes or format code
- 检查perlcritic/perltidy是否已安装
- 对指定文件运行分析
- 按严重级别报告发现的问题
- 提供修复建议或格式化代码
Tool Detection
工具检测
First, verify tools are available:
bash
undefined首先验证工具是否可用:
bash
undefinedCheck 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"
undefinedcommand -v perltidy >/dev/null && echo "perltidy available" || echo "Install: cpanm Perl::Tidy"
undefinedPerl::Critic Analysis
Perl::Critic分析
Running perlcritic
运行perlcritic
bash
undefinedbash
undefinedDefault 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
undefinedperlcritic --exclude RequireUseWarnings script.pl
undefinedSeverity Levels
严重级别
| Level | Name | Description |
|---|---|---|
| 5 | Gentle | Obvious, unambiguous issues |
| 4 | Stern | Usually bad practices |
| 3 | Harsh | Controversial but recommended |
| 2 | Cruel | Pickier about style |
| 1 | Brutal | Maximum strictness |
| 级别 | 名称 | 描述 |
|---|---|---|
| 5 | 温和 | 明显、无歧义的问题 |
| 4 | 严格 | 通常属于不良实践 |
| 3 | 苛刻 | 存在争议但仍推荐遵循 |
| 2 | 严苛 | 对代码风格要求更高 |
| 1 | 最高严格 | 最大严格度 |
Common Policies
常用规则
| Policy | Severity | Issue |
|---|---|---|
| RequireUseStrict | 5 | Missing |
| RequireUseWarnings | 4 | Missing |
| ProhibitBarewordFileHandles | 5 | Using bareword filehandles |
| ProhibitTwoArgOpen | 5 | Two-argument open() |
| ProhibitStringyEval | 5 | Using eval with string |
| RequireTidyCode | 1 | Code not formatted |
| 规则名称 | 严重级别 | 问题 |
|---|---|---|
| RequireUseStrict | 5 | 缺少 |
| RequireUseWarnings | 4 | 缺少 |
| ProhibitBarewordFileHandles | 5 | 使用了裸字文件句柄 |
| ProhibitTwoArgOpen | 5 | 使用了双参数形式的open() |
| ProhibitStringyEval | 5 | 对字符串使用eval |
| RequireTidyCode | 1 | 代码未格式化 |
Configuration File
配置文件
Create in project root:
.perlcriticrcini
undefined在项目根目录创建:
.perlcriticrcini
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
undefinedVerbose Formats
详细输出格式
| Level | Output Format |
|---|---|
| 1 | Line only |
| 4 | Line + Column |
| 8 | Line + Policy + Severity |
| 10 | Full explanation |
| 11 | With PBP page reference |
| 级别 | 输出格式 |
|---|---|
| 1 | 仅显示行号 |
| 4 | 行号 + 列号 |
| 8 | 行号 + 规则名称 + 严重级别 |
| 10 | 完整解释 |
| 11 | 附带PBP页面引用 |
Perltidy Formatting
Perltidy格式化
Basic Usage
基础用法
bash
undefinedbash
undefinedFormat 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
undefinedperltidy -st script.pl | diff -q - script.pl
undefinedCommon Options
常用选项
bash
undefinedbash
undefinedBasic 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
--indent-columns=4
--continuation-indentation=4
--maximum-line-length=100
--vertical-tightness=2
--paren-tightness=1
--brace-tightness=1
script.pl
undefinedperltidy
--indent-columns=4
--continuation-indentation=4
--maximum-line-length=100
--vertical-tightness=2
--paren-tightness=1
--brace-tightness=1
script.pl
--indent-columns=4
--continuation-indentation=4
--maximum-line-length=100
--vertical-tightness=2
--paren-tightness=1
--brace-tightness=1
script.pl
undefinedConfiguration File
配置文件
Create in project root:
.perltidyrctext
undefined在项目根目录创建:
.perltidyrctext
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=/
undefinedCombined Workflow
组合工作流
Lint and Fix
检查并修复
bash
#!/bin/bashbash
#!/bin/bashlint-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
undefinedFILE="$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
undefinedInterpreting 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
undefinedWrong
Wrong
open FILE, $filename;
open FILE, $filename;
Correct
Correct
open my $fh, '<', $filename;
undefinedopen my $fh, '<', $filename;
undefinedSeverity 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
undefinedAdd at top
Add at top
use strict;
use warnings;
undefineduse strict;
use warnings;
undefinedSeverity 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
undefinedInstall both tools
Install both tools
cpanm Perl::Critic Perl::Tidy
cpanm Perl::Critic Perl::Tidy
Verify installation
Verify installation
perlcritic --version
perltidy --version
undefinedperlcritic --version
perltidy --version
undefinedQuick Commands
快捷命令
Lint specific file:
bash
perlcritic --severity 4 --verbose 8 path/to/file.plLint all Perl files in directory:
bash
find . -name '*.pl' -o -name '*.pm' | xargs perlcritic --severity 4Format 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