开发者

What does {} mean in perl?

开发者 https://www.devze.com 2023-03-14 09:24 出处:网络
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.
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

0

精彩评论

暂无评论...
验证码 换一张
取 消