gpt4 book ai didi

perl - 当调用的参数比预期的多时,子程序是否应该嘎嘎作响?

转载 作者:行者123 更新时间:2023-12-04 22:05:59 26 4
gpt4 key购买 nike

子程序在调用时是否应该使用比预期更多的参数,还是应该忽略额外的参数?

#!/usr/bin/env perl
use warnings;
use strict;


sub routine {
my ( $a, $b ) = @_;
return $a * $b;
}

my $reslult = routine( 4, 5, 6 );

最佳答案

一般来说,我不会费心手工编写参数检查,但我认为正确的 react 是呲牙咧嘴。这样你就可以扩展函数以在以后接受可选参数而不会破坏现有的调用者。

如果你的 Perl 足够新,你可以像这样安装和使用 Function::Parameters :

use warnings;
use strict;
use Function::Parameters qw(:strict);

fun routine($x, y) {
return $x * $y;
}

my $result = routine(4, 5, 6); # croaks automatically

(顺便说一句,不要将您的变量称为 $a$b :这些名称由 sort 使用并免于 strict 'vars' 检查。)

关于perl - 当调用的参数比预期的多时,子程序是否应该嘎嘎作响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836567/

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