[ACCEPTED]-What does {} mean in perl?-perl

Accepted answer
Score: 12

{} creates a reference to an empty anonymous 1 hash. Read more here.

Example code:

use Data::Dumper;
my $a = {};
print "a is " . Dumper( $a );
my %b = ();
print "b is " . Dumper( \%b );

Outputs:

a is $VAR1 = {};
b is $VAR1 = {};
Score: 10

{}, in this context, is the anonymous hash 5 constructor.

It creates a new hash, assigns 4 the result of the expression inside the 3 curlies to the hash, then returns a reference 2 to that hash.

In other words,

{ EXPR }

is roughly equivalent 1 to

do { my %hash = ( EXPR ); \%hash }

(EXPR can be null, nothing.)

perlref

More Related questions