my $a = {};
my $b = {$a=>''};
I know {}
can be used to reference a hash key,but what does {}
mean here?
Just as with the =~ regex match operator, the left side is the "subject" of the match, and the right side is the "pattern" to match against -- whether that be a plain scalar, a regex, an array or hash reference, a code reference, or whatever.
$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.
{} 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With