Mod parent up! Oh, wait, I just did that.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.