1. Perl
  2. Operators
  3. here

Anonymous hash generator - create hash reference

Perl has an operator anonymous hash generator "{}". The name is a bit tricky, but in practice it's an easy way to create a hash reference.

With the anonymous hash generator, you can easily create hash reference as follows:

# Anonymous hash generator
my $point = {x => 1, y => 2};

If you don't want to use an anonymous hash generator, write:First create hash and then use the reference generator to create a hash reference.

# If you don't use an anonymous hash generator
my %point = (x => 1, y => 2);
my $point = \%point;

If you use an anonymous hash generator, you can omit the above process and write it. When you look at {x => 1, y => 2}, it will be easier to understand if you have the feeling that the above processing is omitted.

Why is it called an "anonymous hash generator"?

With an anonymous hash generator, you don't have to create a hash called "%point". In other words, an "anonymous hash" that corresponds to "%point" is created internally.

I'm sure it was named with a focus on this part.

There is anonymous array generator that corresponds to the anonymous hash generator.

Related Informatrion