【发布时间】:2018-05-18 10:13:16
【问题描述】:
我是 Perl 的非常初学者。我有一个散列$self,其中包含其他散列,特别是它包含一个带有键params 的散列。我想将与params 的键color 对应的值从black 更新为white。
换句话说,$self 是怎样的:
{
'params' => {
'color' => 'black',
#other keys/values here...
},
#other hashes here...
};
我想要什么:
{
'params' => {
'color' => 'white',
#other keys/values here...
},
#other hashes here...
};
我尝试了什么:
sub updatePrice {
my ($self, $text) = @_; # I can't change this
my %params = %{$self->{params}};
$params{color} = 'white';
print Data::Dumper::Dumper{$params{color}};
# ...
}
但我收到Odd number of elements in anonymous hash 警告,而打印的是$VAR1 = { 'white' => undef };。
我做错了什么?
【问题讨论】:
-
您应该将标记为
# I can't change this的行更改为my $self = shift;和my ($text) = @_;这是很常见的做法,并将显式参数留给@_中的子例程,但删除对象或类接收应该是只读的消息。