Home > database >  Perl: Using data from an anonymous hash
Perl: Using data from an anonymous hash

Time:07-08

I am adding on to another developers code, we are both new to perl I am trying to take a list of IPs and run it through whois. I am unsure how to access the data in the anonymous hash, how do I use it? He told me the data was stored inside one. This is the only instance I could find mentioning a hash:

## Instantiate a reference to an anonymous hash (key value pair)
my $addr = {};

CodePudding user response:

The anonymous hash is the {} part. It returns a reference which can be stored in a scalar variable, like in your example:

my $addr = {};

To see the structure, you can print it with Data::Dumper:

use Data::Dumper;
print Dumper $addr;

It might show you something like:

$VAR1 = {
          'c' => 1,
          'a' => 2
        };

You access the hash keys using the arrow operator:

print $addr->{"a"}

Like how you would access a regular hash, but with the arrow operator in between.

You can dereference the reference by putting a hash sigil in front

%$addr

# compare            
  • Related