Reference to anonymous hash

A reference to an anonymous hash can be created using curly brackets:

$hashref = {
'Adam'  => 'Eve',
'Clyde' => 'Bonnie',
};

Because curly brackets (braces) are used for several other things including BLOCKs, you may occasionally have to disambiguate braces at the beginning of a statement by putting a "+" .


The leading "+{" and "{;" always serve to disambiguate the expression to mean either the HASH reference, or the BLOCK.

Example :

$ cat ref_hash.pl 
$\="\n";
$,=" ";
$hashref = {
'Adam'  => 'Eve',
'Clyde' => 'Bonnie',
};

print %$hashref;


Output :

$ perl ref_hash.pl 
Clyde Bonnie Adam Eve
$

 

Last modified: Monday, 2 April 2012, 02:16 AM

Skip Navigation

Navigation

Skip OpenGurukulSkip Settings

Settings