in reply to Re: OO - inside-out or by means of a anonymous sub?
in thread OO - inside-out or by means of a anonymous sub?

My take on typo checking is that attributes should be accessed by variables (or constants) rather than by strings. Coincidentally, that makes indexing an arrayref just as convenient as indexing a hashref.

$self->{attribute} becomes $self->[ATTRIBUTE] (where ATTRIBUTE is an integer constant). Those constants should not be visible outside of the package published or exported, because they're an implementation detail.

Admittedly, the initialization code becomes a bit less beautiful, but it's still perfectly clear:

sub new { my ($class) = @_; my @obj; @obj[ACCOUNT, NAME, BALANCE] = (undef, undef, 0); return bless( \@obj, $class); }
Variations on this that avoid declaring an array would probably be uglier.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3: OO - inside-out or by means of a anonymous sub?
by ikegami (Patriarch) on Jan 12, 2006 at 16:30 UTC
    I use the method you describe as well. However, I'm curious how you hide the constants from other packages. ("Those constants should not be visible outside of the package") It could be done if they were lexical variables, but there's no $ on your constants.