Oh, I'd definitely make that an object. If only so I could either make normalized_designation a computed attributed, or at least write a set_normalized_designation
that looked something like:
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";
}
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.
Of course, in Perl 6 you can have your cake and eat it:
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;
}
}
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.
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. |