如何在启用了Subroutines::ProhibitExcessComplexity
的脚本中运行Perl评论家?我尝试了以下但它没有标记复杂的500多行子程序。
我没有.perlcriticrc
(我也不希望有人在那里)
use Perl::Critic;
run_critic('temp.pl');
exit;
sub run_critic {
my $file = shift;
my $critic = Perl::Critic->new(
-severity => 5,
-policy => 'Subroutines::ProhibitExcessComplexity',
-verbose => 5,
);
my @violations = $critic->critique($file);
print @violations;
}
我的Perl::Critic
版本为1.116
perl -MPerl::Critic -e 'print "$Perl::Critic::VERSION\n"'
1.116
答案 0 :(得分:2)
要查看实际加载的策略,请使用Perl::Critic->policies
。
政策()
返回一个列表,其中包含对已加载到此引擎的所有Policy对象的引用。对象将按其加载的顺序排列。 如果您查看自己的政策,则可以看到它未被添加:
您将看到所需的政策未添加到列表中:
use strict;
use warnings;
use Perl::Critic;
run_critic($0);
exit;
sub run_critic {
my $file = shift;
my $critic = Perl::Critic->new(
-severity => 5,
-policy => 'Subroutines::ProhibitExcessComplexity',
-verbose => 5,
);
print $critic->policies(); # View loaded Policies
my @violations = $critic->critique($file);
print @violations;
}
输出:
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
-include
问题在于Constructor option -policy
不存在。
相反,您想使用-include
:
-include
是对字符串@PATTERNS
列表的引用。无论其他所有设置如何,始终会加载至少匹配一个m/$PATTERN/ixms
的策略模块。例如:
因此修复构造函数:
my $critic = Perl::Critic->new(
-severity => 5,
-include => 'Subroutines::ProhibitExcessComplexity',
-verbose => 5,
);
输出:
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::ProhibitExcessComplexity # <--- ta da
Subroutines::ProhibitExplicitReturnUndef
Subroutines::ProhibitNestedSubs
Subroutines::ProhibitReturnSort
Subroutines::ProhibitSubroutinePrototypes
TestingAndDebugging::ProhibitNoStrict
TestingAndDebugging::RequireUseStrict
ValuesAndExpressions::ProhibitLeadingZeros
Variables::ProhibitConditionalDeclarations
Variables::RequireLexicalLoopIterators
答案 1 :(得分:1)
Subroutines::ProhibitExcessComplexity
的严重性为3(从源代码中可以看出),但您使用的是5.这意味着您只显示违反严重性= 5的情况。将-severity
设置为1,2或3:
my $critic = Perl::Critic->new(-severity => 1);
另请注意,-policy
不是POD中new
的选项;它被忽略了。
答案 2 :(得分:0)
我建议改用perlcritic
实用程序。它可以做很好的事情,比如将输出着色或者将它发送给寻呼机。然后你可以用shell脚本,别名或反引号,或system()
调用或任何你喜欢的东西来包装它。
# Analyze with just the ProhibitExcessComplexity policy
perlcritic --single-policy=ProhibitExcessComplexity FILES_OR_DIRECTORIES
# Analyze with all severity 5 policies, plus ExcessComplexity
perlcritic --severity=5 --include=ProhibitExcessComplexity FILES_OR_DIRECTORIES