请查看代码: 我想查看功能的输入参数,在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;
}
[codereview.stackexchange.com](http://codereview.stackexchange.com) –
我会在问题中生活旧版本,否则没有人会理解答案。其次,你不必去做所有Perl :: Critic说的。如果写入控制台而不是文件,检查'say'的返回值有点太多。 – Matteo
's /使用5.01 /使用5.010 /'相当,但更清晰。 –