in reply to avoiding hash key typos

What I do is treat hashkeys like radioactive waste: I handle them directly as little as possible. If I can programmatically generate them in one way or another, I do. If not, I limit my "direct contact" with them (i.e. cases where I actually type out a hash key) to a single initial typing of the key (usually as one in a list of arguments to qw). (Typing each key once is usually OK; it's when one has to type the same key more than once that troubles begin.)

For fields of hashref-based objects, I avoid accessing the hash slot directly, and define accessors instead. A mistyped hashkey won't trigger an error from Perl, but a mistyped accessor will.

If a program's design is such that multiple typing of the same key is unavoidable, I begin thinking about how to restructure the code.

the lowliest monk

Replies are listed 'Best First'.
Re^2: avoiding hash key typos
by DrWhy (Chaplain) on Apr 15, 2005 at 14:02 UTC
    This is a specific case of a general principle of software design: If you have to do something more than once, abstact it. (put a string in a variable, turn a repeated block of code into a subroutine, put identical subs in multiple scripts into a library, etc.)

    I don't know how extensive your script, but if it is relatively simple, I would avoid using modules and specially designed tie's when the basic tools of the language suffice. Assign the keys to variables once and then use the variables to do hash accesses. If your real code is more complex, you might look at Hash::Util or another appropriate module, or use fields with OO accessors as was already suggested.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re^2: avoiding hash key typos
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 21:49 UTC
    >> I limit my "direct contact" with them (i.e. cases where I actually type out a hash key) to a single initial typing of the key...

    --To address things by names, some people also use OO languages with classes and data members:-).
    (Which are implemented internaly as hashes of course...:-)).