gpt4 book ai didi

perl - 模块以 "1;"结尾,perlcritic 提示它没有

转载 作者:行者123 更新时间:2023-12-02 02:45:14 27 4
gpt4 key购买 nike

有一个简单的模块

package Rrr;
use 5.014;
use warnings;
use namespace::sweep;
use Moo;
use Method::Signatures::Simple;

BEGIN {
our $VERSION = '0.0.1';
}

has 'root' => (
is => 'rw',
default => 'root'
);

method func {
say 'This is the func method from ' . __PACKAGE__ . ' with value: ', $self->root;
}

1;

perlcritic -1

Code is not tidy at line 1, column 1.  See page 33 of PBP.  (Severity: 1)
Module does not end with "1;" at line 17, column 1. Must end with a recognizable true value. (Severity: 4)
Return value of flagged function ignored - say at line 18, column 5. See pages 208,278 of PBP. (Severity: 1)

如何让perlcritic高兴?

编辑 - 基于@toolic的评论

是的,整洁有助于解决第一个问题(但是代码在第 1 行第 1 列不整洁。 没有多大帮助的消息),因为差异是:

13c13
< is => 'rw',
---
> is => 'rw',
18c18,19
< say 'This is the func method from ' . __PACKAGE__ . ' with value: ', $self->root;
---
> say 'This is the func method from ' . __PACKAGE__ . ' with value: ',
> $self->root;

但仍然得到:

Module does not end with "1;" at line 17, column 1.  Must end with a recognizable true value.  (Severity: 4)
Return value of flagged function ignored - say at line 18, column 5. See pages 208,278 of PBP. (Severity: 1)

我的批评者:

$ perlcritic --version
1.125

最佳答案

看起来 Method::Signatures::Simple 中的 method 关键字正在抛出 perlcritic。请注意 PPI 解析以下程序的方式的差异:

$ tools/ppidump 'method foo { 1 } 1;'
PPI::Document
PPI::Statement
[ 1, 1, 1 ] PPI::Token::Word 'method'
[ 1, 8, 8 ] PPI::Token::Word 'foo'
PPI::Structure::Block { ... }
PPI::Statement
[ 1, 14, 14 ] PPI::Token::Number '1'
[ 1, 18, 18 ] PPI::Token::Number '1'
[ 1, 19, 19 ] PPI::Token::Structure ';'

$ tools/ppidump 'sub foo { 1 } 1;'
PPI::Document
PPI::Statement::Sub
[ 1, 1, 1 ] PPI::Token::Word 'sub'
[ 1, 5, 5 ] PPI::Token::Word 'foo'
PPI::Structure::Block { ... }
PPI::Statement
[ 1, 11, 11 ] PPI::Token::Number '1'
PPI::Statement
[ 1, 15, 15 ] PPI::Token::Number '1'
[ 1, 16, 16 ] PPI::Token::Structure ';'

使用方法时,整个程序被视为单个语句;使用 sub 时,1; 被视为单独的语句。

为了使 perlcritic 安静,您可以在方法的右大括号后添加分号:

method func {
...
};

1;

或者

method func {
...
}

;1;

但是,我认为amon在评论中提出了一个很好的观点:

perlcritic can't handle syntax extensions such as method signatures...Due to such issues, I tend to choose between syntax extensions and perlcritic – and sadly have to prefer static analysis over syntactic sugar in most cases.

关于perl - 模块以 "1;"结尾,perlcritic 提示它没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com