in reply to Quoted hask keys (was: Re^2: What to test in a new module)
in thread What to test in a new module
In my experience, there are two ways of using hashes. One is as a lazy-programmer's object. You see these kinds of hashes returned from database queries, received from REST API requests, etc. Something like this:
my $uncle = { name => 'Bob', age => 45, };
In these cases, the keys are an already-known set of strings, usually picked to be identifier-like (no spaces, weird punctuation, etc), so I will usually not quote keys in this kind of hash.
The other way of using hashes is as basically a mapping from values of one type to values of another type. For example:
my %ages = ( 'Alice' => 41, 'Bob' => 45, 'Carol' => 38, );
Quoting the keys for this kind of hash is useful because you never know when another value is going to be added which isn't a safe identifier ($ages{"d'Artagnan"} = 62) plus it helps visually distinguish the second kind of hash from the first kind of hash.
Do I consistently follow this rule? Absolutely not.
|
|---|