如何在启用了ProhibitExcessComplexity的脚本中运行perl评论家?

时间:2014-10-22 19:12:53

标签: perl perl-critic

如何在启用了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

3 个答案:

答案 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