in reply to Re: Using the strict module in object oriented programming
in thread Using the strict module in object oriented programming

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.

Replies are listed 'Best First'.
Re^3: Using the strict module in object oriented programming
by imp (Priest) on Jul 26, 2006 at 02:20 UTC
    Damian's book (Perl Best Practices) is full of useful content. Some of it takes getting used to of course (e.g. always using the 'x' modifier for regex).

    I agree with you that restricted hashes are not inherently bad - they just aren't reliable for security.

    In section 15.4 he does say "Don't use restricted hashes", but he also spends several paragraphs talking about their benefits.

Re^3: Using the strict module in object oriented programming
by mrguy123 (Hermit) on Jul 26, 2006 at 07:40 UTC
    Thanks, that sounds like good advice, and I think I will use it.