Can anyone explain the hashes in Perl?

The main function:

my %hash = {'inner1'=>{'foo'=>5},
            'inner2'=>{'bar'=>6}};
$object->State(0, %AMSValues);

Sent to:

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

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

Hash values

It doesn't seem like the image works like this

$value = $value->{"HASH(0x52e0b6c)"}
%values = $values->{"HASH(0x52e0b6c)"}
+3
source share
1 answer

use warnings; is always.

Your

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

wrong; {}generates an anonymous hash link, and% hash gets one key (this hash link is gated) and the value is undef.

You wanted:

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

Regarding the transition to routines, you cannot pass hashes; the code, as you show, aligns the hash in the list, and then collects the hash from again @_, but it will be a separate copy. If you really want to use the same hash, you must pass the hash link instead.

+13
source

All Articles