4.5 Braces, Brackets, and Quoting
In the previous section we pointed out that $push = "pop on "; print "${push}over";
print out " print ${push} . 'over'; or even: print ${ push } . 'over';
will also print " $hash{ "aaa" }{ "bbb" }{ "ccc" } you can just write: $hash{ aaa }{ bbb }{ ccc } and not worry about whether the subscripts are reserved words. In the rare event that you do wish to do something like: $hash{ shift } you can force interpretation as a reserved word by adding anything that makes it more than a mere identifier: $hash{ shift() } $hash{ +shift } $hash{ shift @_ }
The
-w
switch will warn you if it
interprets a reserved word as a string, since you may have
meant
the reserved word. (That's why we
recommend you use 4.5.1 Hard References Don't Work as Hash KeysConsistent with the foregoing, hash keys are stored internally as strings.[ 7 ] If you try to store a hard reference as a key in a hash, the key value will be converted into a string:
$x{ \$a } = $a; ($key, $value) = each %x; print $$key; # WRONG
We mentioned earlier that you can't convert a string back to a hard
reference. So if you try to dereference $r = \@a; $x{ $r } = $r; And then at least you can use the hash value , which will be a hard reference, instead of the key, which won't. Although you can't store a hard reference as a key, if you use a hard reference in a string context, it is guaranteed to produce a unique string, since the address of the reference is included as part of the resulting string. So you can in fact use a hard reference as a unique hash key. You just can't dereference it later. |
|