检查功能输入值是否正确?

时间:2011-11-01 08:08:17

标签: perl input parameters

请查看代码: 我想检查功能的输入参数,在PBP中提供的Domian Conway使用croak,是否正确? 我改变代码来传递http://perlcritic.com/但是 可以添加     $ rs =说“$ 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 使用模块Attribute :: Signature也因错误而不适用 CODE包属性可能与未来的保留字冲突:返回第46行 CODE包属性可能与将来的保留字冲突:with:return at - line 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 个答案:

答案 0 :(得分:5)

一些评论

  • 使用严格和警告它可以帮助您检测错误

    use strict;
    use warnings;
    
  • 您可以考虑查看Perl::Critic(有online版本)

  • 总是声明变量:even loop iterators

    for my $test_val (@test) {   
    
  • 单引号中的
  • 字符串未进行插值($打印为$)。使用双引号:

    say "$r->{foo} > 100";
    
  • croak终止程序。由于在您测试中您想要使用carp检查多个值:它会打印警告但是继续

  • 总是在子程序结束时使用return:它将有助于明确返回的内容(否则perl会自动返回上次执行的评估的结果)

答案 1 :(得分:4)

我看到的第一个问题:

  • 顶部没有use strict; use warnings;
  • say参数中的单引号可防止变量被扩展

试试Code Review

答案 2 :(得分:1)

我曾使用Attribute::Signature一段时间,甚至发布了新版本。但后来我决定对我来说sub params太罕见了,所以我决定停止使用它。

相关问题