What does {} mean in perl?
my $a = {};
my $b = {$a=>''};
I know {}
开发者_开发问答can be used to reference a hash key,but what does {}
mean here?
{} creates a reference to an empty anonymous 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 = {};
{}
, in this context, is the anonymous hash constructor.
It creates a new hash, assigns the result of the expression inside the curlies to the hash, then returns a reference to that hash.
In other words,
{ EXPR }
is roughly equivalent to
do { my %hash = ( EXPR ); \%hash }
(EXPR
can be null, nothing.)
perlref
精彩评论