Because the intent of this feature is primarily to let you define subroutines that work like built-in functions, here are prototypes for some other functions that parse almost exactly like the corresponding built-in. 在其他情况下,使用Prototype是危险的!
再来看一个例子
use List::Util qw( min max );
sub clip_to_range($$@) {
my ($min, $max, @data) = @_;
return map { max( $min, min($max, $_) ) } @data;
}
clip_to_range(1, 2, (5..10));
#min是1, max是2,data是(5..10)
my $range_ref = [1, 2]; #独立定义了一个range
clip_to_range(@{$range_ref}, (5..10));
#使用这个range, 想表达和上面一次调用相同的意思,但是...
#min是2, max是5,data是(6..10)
为什么min是2呢? 因为@{$range_ref}被当成了第一个scalar参数,被转换成了长度,所以是2。而max则应该是下一个参数,Perl就从(5..10)中把5当做了max,余下的(6..10)就作为data。
程序真正执行的和自己预期的不相同,往往容易导致产生了Bug但是查不出来,因此最好避免这种情况的发生。上面的这个例子正是《Perl Best Practices》中的一条:“Don't use subroutine prototypes.”