In perl, hashes are represented by variables whose names begin with a
percent sign (%
). Just as with arrays, however, the percent sign
is only used when you're refering to the entire hash. To access a
element of a hash (which must be a scalar), begin the name with a dollar
sign ($
) just as with an array, but, instead of using square
brackets to enclose the index, use curly braces ({
and
}
) to enclose the index.
Perl treats barewords inside these curly
braces a little differently than the way described in
Section 3.1; barewords are treated as character strings
even if there is a function with the same name. This rule applies only
to strings without blanks or other special characters; if a key contains
such characters it must be enclosed in quotes, with the usual rules for
quoted strings (Section 3.1) in effect. Of course, you can
always enclose string values for keys in quotes, and many perl programmers
do just that.
If you refer to a scalar element of a hash, perl will create the hash if it
doesn't already exist. If you want to explicitly create an empty hash,
use an empty set of parentheses(()
).