1. Perl
  2. Operators
  3. here

Anonymous array generator [] - Easily create array reference

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

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

# Anonymous array generator
my $nums = [1, 2, 3];

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

# If you don't use an anonymous array generator
my @nums = (1, 2, 3);
my $nums = \@nums;

If you use an anonymous array generator, you can omit the above process and write it. When you look at [1, 2, 3], it will be easier to understand if you have the feeling that the above processing is omitted.

Why is it called an "anonymous array generator"?

With the anonymous array generator, you don't have to create an array called "@nums". In other words, an "anonymous array" corresponding to "@nums" is created internally.

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

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

Related Informatrion