我正试图找出在Perl中区分参数未传递和参数传递为0的最佳方法,因为它们对我来说意义不同。
(通常我喜欢这种模糊性,但在这种情况下,我正在生成SQL,所以我想用NULL替换未定义的参数,但将0保留为0。)
所以这就是歧义:
(通常我喜欢这种模糊性,但在这种情况下,我正在生成SQL,所以我想用NULL替换未定义的参数,但将0保留为0。)
所以这就是歧义:
sub mysub {
my $arg1 = shift;
if ($arg1){
print "arg1 could have been 0 or it could have not been passed.";
}
}
到目前为止,这是我最好的解决方案......但我认为它有点丑。我想知道你是否能想到更简洁的方法或者你觉得这个看起来还可以:
sub mysub {
my $arg1 = (defined shift) || "NULL";
if ($arg1 ne "NULL"){
print "arg1 came in as a defined value.";
}
else {
print "arg1 came in as an undefined value (or we were passed the string 'NULL')";
}
}