开发者

Can someone explain hashes in Perl?

Main Function:

my %hash = {'inner1'=>{'foo'=&g开发者_如何学运维t;5},
            'inner2'=>{'bar'=>6}};
$object->State(0, %AMSValues);

Sent to:

sub State
{
   my ($self, $state, %values) = @_;
   my $value = \%values;

From what I know one should be a hash and the other is a pointer, but...

Can someone explain hashes in Perl?

It doesn't look like the picture is working so,

$value = $value->{"HASH(0x52e0b6c)"}
%values = $values->{"HASH(0x52e0b6c)"}


use warnings; always.

Your:

my %hash = {'inner1'=>{'foo'=>5},
            'inner2'=>{'bar'=>6}};

is incorrect; {} generates an anonymous hash reference, and %hash gets a single key (that hash reference stringified) and a value of undef.

You wanted:

my %hash = ('inner1'=>{'foo'=>5},
            'inner2'=>{'bar'=>6});

As far as passing to subroutines goes, you can't pass hashes; code like you show flattens the hash into a list and then reassembles a hash from @_, but that will be a separate copy. If you actually want the same hash, you must pass a hash reference instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜