Anyway, most likely all you need to do is a use Hash::Util qw(lock_keys); and then at the end of each of your new subs do a lock_keys(%{$self}); return $self;
Hash::Util is a standard module for perl 5.8 and up, so you don't need to do any installation, and there's no particular performance penalty for doing this either. This is exactly what you're looking for, a way to strictify the fields of a hash and catch spelling errors.
Everyone else here is telling you interesting and useful things (in some combination of the two), but don't get distracted by them, because this particular "way to do it" is most likely the one you're after (Damien Conway did the perl world a great disservice by dissing "lock_keys", if you ask me).
Note: The actual requirement for "lock_keys" is that it must be done after the hash fields have been defined and after the hashref has been blessed. If, for example, you don't have access to the code that generates the objects, you can do a "lock_keys" later, inside of each method that you're working with. If you go for a local "lock_keys" approach, I strongly suggest doing an "unlock_keys" at the end of the method, particularly if you're returning "$self" to allow chained method calls. Some of the existing code may expect to be able to create new keys on the fly.
In reply to Re^2: Using the strict module in object oriented programming
by doom
in thread Using the strict module in object oriented programming
by mrguy123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |