gpt4 book ai didi

perl - 这个 Perl 表达式有什么问题?

转载 作者:行者123 更新时间:2023-12-04 09:32:50 24 4
gpt4 key购买 nike

跟随有什么问题。我收到 $attribute not defined错误。

if (my $attribute = $Data->{'is_new'} and $attribute eq 'Y') {
}

最佳答案

use strict;就会发现问题。

$ perl -e'use strict; my $attribute = "..." and $attribute eq "Y";'
Global symbol "$attribute" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.

A my声明只对后面的语句有影响,对声明所在的语句没有影响。 (同样适用于 ourlocal 声明。)这意味着 $attribute您使用 my 创建的并且您分配的变量与 $attribute 不同你比较 Y .你要
my $attribute = $Data->{'is_new'};
if ($attribute eq 'Y') { ... }

现在,如果 $Data->{is_new}不存在或未定义, $attribute将是未定义的,并将其与 Y 进行比较会发出警告。您可以通过以下方式避免此警告:
my $attribute = $Data->{'is_new'};
if (defined($attribute) && $attribute eq 'Y') { ... }

或者:(5.10+)
my $attribute = $Data->{'is_new'};
if (($attribute // '') eq 'Y') { ... }

关于perl - 这个 Perl 表达式有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426520/

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