2011-11-01 86 views
1

请查看代码: 我想查看功能的输入参数,在PBP中的Domian Conway提供使用croak,它是正确的吗? 我将代码更改为通过http://perlcritic.com/,但可能会添加 $ rs = say“$ r - > {foo} < = $ check_value”; 过剩它是检查功能输入值的正确方法吗?

#!/usr/bin/perl 
############################################################################## 
#  $URL: http://mishin.narod.ru $ 
#  $Date: 2011-11-01 16:32:04 +0400 (Nov, 01 Nov 2011) $ 
# $Author: mishnik $ 
# $Revision: 1.02 $ 
# $Source: test check variables $ 
# $Description: check input parameters of function 
# $ Domian Conway in PBP offer to use croak, is it correct? $ 
# 01-11-2011: 
# put question to 
# http://stackoverflow.com/questions/7963866/is-it-correct-way-to-check-function-input-values 
############################################################################## 

use strict; 
use warnings; 
use 5.010; 

use Carp qw(cluck carp); 
use Data::Dumper; 
use Readonly; 
use autodie; 

our $VERSION = '0.01'; 

Readonly my $CHECK_LEVEL => 100; 

my %filials; 
my $ref_hash = \%filials; 
my @test  = qw/444 33a 2 d 300 ffd 22/; 
my $ret; 
for my $test_val (@test) { 
    $filials{foo} = $test_val; 
    $ret = test_var(\%filials, $CHECK_LEVEL) 
     || carp("couldn't invoke test_var \%filials, $CHECK_LEVEL "); 
} 

sub test_var { 
    my $r   = shift; 
    my $check_value = shift; 

    #check if input parameters is correct 
    carp("ERROR: \$r->{foo} is not defined or not number, \$r:\n" . Dumper($r)) 
     if !defined $r->{foo} 
      || $r->{foo} !~ /^\d+$/xms; 

    #check values by business rule 
    if ($r->{foo} > $check_value) { 
     say "$r->{foo} > $check_value"; 
    } 
    else { 
     say "$r->{foo} <= $check_value"; 
    } 
    return 1; 
} 

所以 我perlcritic_profile.perlcriticrc只有

severity = 1 

[-InputOutput::RequireCheckedSyscalls] 

旧版本

use 5.01; 
use Carp; 
use Data::Dumper; 
my %filials; 
$filials{boo} = 200; 
$filials{foo} = 300; 
my $ref_hash = \%filials; 
my @test  = qw/444 33a 2 d 300 ffd 22/; 

for $test_val (@test) { 
    $filials{foo} = $test_val; 
    test_var(\%filials); 
} 

sub test_var { 
    my $r = shift; 
    croak( "Value \$r->{foo}***$r->{foo}*** is not defined or not number." 
      . "\nDump \$r=" 
      . Dumper($r)) 
     if !defined $r->{foo} 
      || $r->{foo} !~ /^\d+$/; 
    say $r->{foo}; 
    if ($r->{foo} > 100) { 
     say '$r->{foo} > 100'; 
    } 
    else { 
     say '$r->{foo} <= 100'; 
    } 
} 

2Alexandr Ciornii 使用模块属性::签名也不适宜,因为一个错误 CODE包属性可能与将来的保留字冲突:ret在返回 - 线60

#!/usr/bin/perl 
############################################################################## 
#  $URL: http://mishin.narod.ru $ 
#  $Date: 2011-11-01 16:32:04 +0400 (Nov, 01 Nov 2011) $ 
# $Author: mishnik $ 
# $Revision: 1.02 $ 
# $Source: test check variables $ 
# $Description: check input parameters of function 
# $ Domian Conway in PBP offer to use croak, is it correct? $ 
# 01-11-2011: 
# put question to 
# http://stackoverflow.com/questions/7963866/is-it-correct-way-to-check-function-input-values 
############################################################################## 

use strict; 
use warnings; 
use 5.010; 

use Carp qw(cluck carp); 
use Data::Dumper; 
use Readonly; 

#use autodie; 
use Attribute::Signature; 

our $VERSION = '0.01'; 

#run main procedure 
main(); 

sub main : returns(integer) { 

    #make test for chack input parameters 
    Readonly my $CHECK_LEVEL => 100; 
    my %filials; 
    my $ref_hash = \%filials; 
    my @test  = qw/444 33a 2 d 300 ffd 22/; 
    my $ret; 
    for my $test_val (@test) { 
     $ref_hash->{foo} = $test_val; 
     $ret = test_var($ref_hash->{foo}, $CHECK_LEVEL) 
      || carp("couldn't invoke test_var \%filials, $CHECK_LEVEL "); 
    } 

    return 1; 
} 

sub test_var : with(integer, integer) returns(integer) { 
    my $evaluated_value = shift; 
    my $check_value  = shift; 

    #check values by business rule 
    if ($evaluated_value > $check_value) { 
     say "$evaluated_value > $check_value"; 
    } 
    else { 
     say "$evaluated_value <= $check_value"; 
    } 
    return 1; 
} 
+3

[codereview.stackexchange.com](http://codereview.stackexchange.com) –

+1

我会在问题中生活旧版本,否则没有人会理解答案。其次,你不必去做所有Perl :: Critic说的。如果写入控制台而不是文件,检查'say'的返回值有点太多。 – Matteo

+2

's /使用5.01 /使用5.010 /'相当,但更清晰。 –

回答

4

第一问题我看到: - 在瓮线46个 代码包属性可以与将来的保留字冲突:与

  • 没有use strict; use warnings;在顶部
  • say参数中的单引号保持变量不被扩大

尝试Code Review

+0

我觉得使用5.01;自动添加此,但我错了,谢谢 –

5

一些评论

  • 使用严格和警告,它会帮助你发现错误

    use strict; 
    use warnings; 
    
  • 你可能会考虑检查Perl::Critic(有一个online版)

  • 始终声明变量:偶数循环迭代器

    for my $test_val (@test) { 
    
  • 串在单引号不内插(一个$打印为$)。使用双引号:

    say "$r->{foo} > 100"; 
    
  • croak终止程序。因为在你测试你想检查多个值使用:它会打印一个警告,但继续

  • 总是在子程序的末尾使用return:这将有助于作出明确返回什么(否则自动perl的返回我用Attribute::Signature了一段时间,最后进行评估的结果)

+0

谢谢我让代码通过http://perlcritic.com/ 和鲤鱼:是我需要的超级功能 –

+0

现在,你认为一切都好吗? –

+1

不要检查say的返回值。写入标准输出时几乎不会失败。无论如何,如果你不能写出标准输出,就不可能产生错误。检查打印返回值/说当你写一个文件(在这种情况下,可能会出错) – Matteo

1

,甚至发布了一个新版本。但后来决定,对于我来说子参数是一个难得的问题,所以我决定停止使用它。

+0

我试试这个模块,但我有一个问题 –

相关问题