in reply to Re: Re: Re: Perl Programming guidlines/rules
in thread Perl Programming guidelines/rules
You could certainly argue that this might be considered long winded, but writing things this way makes assumptions explicit and ensure that they're enforced.sub set_normalized_designation { my $self = shift; my $designation = shift; $self->ensure_normalization($designation); $self->{normalized_designation} = shift; } sub ensure_normalization { my $self = shift; my $designation = shift; $designation =~ /.../ or die "$designation is not correctly normalized"; }
Of course, in Perl 6 you can have your cake and eat it:
Now, I might be a little behind on Perl 6 OO, but ISTR that setting is rw on object attributes will autogenerate accessors. If not I'm sure someone will subclass Object to do the right thing.class Thing { my $.title is rw; my $.standard_directory is rw; # Where graphics etc are found my $.designation is rw; my Hash $.def; # Use another class? method normalized_designation { my $normalized_des = $.designation; $normalized_des =~ s/.../.../; return $normalized_des; } }
Note that, in not much more space than it took to comment your hash you have an object, and, for free you get a restricted set of keys, and your dependent field removed and replaced with a computation, and you've got somewhere to hang common behaviour relating to this data structure.
Damn, but I want Perl 6 soon.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Re: Re: Perl Programming guidlines/rules
by mirod (Canon) on Nov 22, 2002 at 15:33 UTC |