为什么这个语句返回 true
为什么这个语句
if (! $ssh_options{user}) {
delete $ssh_options{user};
}
返回true,但是这个语句
if ($ssh_options{user} eq 'undef') {
delete $ssh_options{user};
}
给了我错误,
Use of uninitialized value $ssh_options{"user"} in string eq at analyze.pl line 230.
我认为你总是需要有一些东西让perl来比较被比较的变量的值。
**更新**
@Quick Joe Smith
我无法根据哈希键是否存在进行比较,因为该哈希值
sub ssh_connect {
my $host = shift;
my %ssh_options = (
port => shift,
user => shift,
password => shift
);
来自调用子例程的函数
if ((exists $config_file{user}) && (exists $config_file{password})) {
my $vmware_user = $config_file{user};
my $vmware_password = $config_file{password};
ssh_connect($vmware_host, $vmware_port, $vmware_user, $vmware_password);
} else {
ssh_connect($vmware_host, $vmware_port);
}
Why does this statement,
if (! $ssh_options{user}) {
delete $ssh_options{user};
}
return true, but this statement
if ($ssh_options{user} eq 'undef') {
delete $ssh_options{user};
}
give me the error,
Use of uninitialized value $ssh_options{"user"} in string eq at analyze.pl line 230.
I thought you always had to have something for perl to compare the value of the variable being compared.
** Update **
@ Quick Joe Smith
I cannot base my comparison on whether the hash keys exist or not because the values for this hash
sub ssh_connect {
my $host = shift;
my %ssh_options = (
port => shift,
user => shift,
password => shift
);
Come from this function which calls the sub routine
if ((exists $config_file{user}) && (exists $config_file{password})) {
my $vmware_user = $config_file{user};
my $vmware_password = $config_file{password};
ssh_connect($vmware_host, $vmware_port, $vmware_user, $vmware_password);
} else {
ssh_connect($vmware_host, $vmware_port);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Perl 哈希槽有多种可能为假的方式。如果该键从未被填充过,则
exists
函数将返回 false。如果键存在但包含未定义的值,则exists
将为 true,但defineed
函数将返回 false。在正常的布尔上下文中,由if
语句给出,从未填充或未定义的哈希键、''
、0
或任何减少到这些的东西都是错误的。从您的更新来看,听起来您想要的是使用
define
Perl hash slots have a number of ways they can be false. If the key has never been filled, the
exists
function will return false. If the key exists but contains an undefined value,exists
will be true but thedefined
function will return false. In normal boolean context, as given by anif
statement, a hash key that was never filled, or is undefined,''
,0
or anything that reduces to those will be false.From your update, it sounds like what you want is to use
defined
使用
exists
检查哈希键。但是,在您的情况下,您似乎正在检查其值可能未定义的现有键,在这种情况下:
上面的内容可能就是您想要的。
作为旁注,检查任何变量的值是否按照您描述的方式定义:
是错误的。您正在检查变量是否包含字符串“undef”。在这些情况下你需要的是:
Use
exists
for checking hash keys.However, in your case, it seems as though you're checking for existing keys whose values may be undefined, in which case:
The above may be what you want.
As a sidenote, checking to see if the value of any variable is defined in the way you described:
Is wrong. You're checking to see if the variable contains the string "undef". What you need in those situations is:
但是,如果 $ssh_options{"user"} 未初始化,则它没有可比较的值。
But if
$ssh_options{"user"}
is uninitialized then it it doesn't have a value to compare to.对于 Perl,undefined 在布尔上下文中计算结果为 false。所以您的第一个测试如您所期望的那样“有效”。但是,如果零或空字符串是
$ssh_options{user}
保存的合法值(如果您不希望将其与 undef 相同),则可能会引入错误。这是因为您的第一个测试不区分 false 作为值和 false 作为未定义。就 Perl 的布尔计算而言,以下所有内容都是“假”:0
、''
(空字符串)、undef
或一个空列表。您的第二个代码片段失败,因为
undef
应该是裸字,而不是带引号的字符串。 'undef' 作为带引号的字符串是一个值,在布尔测试中实际上会计算为 'true'。因为您正在将实际值与散列元素进行比较,所以 Perl 会警告您正在将某些内容与未定义的值进行比较。在本例中,未定义的值为$ssh_options{user}
。 Perl 警告你实际上是件好事;它给了你一个线索,告诉你你做错了什么。如果您确实想测试 $ssh_options{user} 是否已定义,请使用 Defined() 函数。如果要测试$ssh_options{user}是否存在,请使用exists()函数。
With Perl, undefined evaluates to false in Boolean context. So your first test 'works' as you expect. However, it might introduce a bug if zero or an empty string is a legitimate value for
$ssh_options{user}
to hold, if you don't wish to treat that the same as undef. That's because your first test doesn't differentiate between false as a value, and false as undefined. As far as Perl's Boolean evaluation goes, all of the following are "false":0
,''
(empty string),undef
, or an empty list.Your second snippet of code fails because
undef
should be a bareword, not a quoted string. 'undef' as a quoted string IS a value, which would actually evaluate to 'true' in Boolean tests. Because you're comparing an actual value to your hash element, Perl warns you that you're comparing something to an undefined value. In this case, the undefined value is$ssh_options{user}
. It's actually good that Perl is warning you; it's given you a clue as to what you're doing wrong.If you really want to test whether $ssh_options{user} is defined, use the defined() function. If you want to test whether $ssh_options{user} exists, use the exists() function.