Distractions with Class::BuildMethods

Ovid on 2006-10-26T22:13:30

Given that a dear friend of mine is suffering from a personal tragedy which has impacted me quite heavily, I've not been terribly responsive to email lately. If I've ignored you, please don't take it personally.

To distract myself, I've been updating Class::BuildMethods to handle class data. I really didn't want to extend this module, but class data is too important to ignore. If I get more tuits, I'll also make it serializable via Storable, but with the caveat that if there's any state data stored outside of the interface, it won't be able to handle it.

The serialization issue annoys me a bit because I keep getting criticisms that this module doesn't allow one to easily serialize data, but since it was designed to add state data to existing classes regardless of implementation, I didn't think it was important since I could not guarantee that one could serialize all data. However, if all data is set via this module, then the serialization is appropriate.

If I get even more tuits, I'll build a threaded Perl and see if I can make it thread safe. However, I have a friend from the US visiting next week and we're off to Dublin, so no guarantees.

I've also discovered that I rarely use its validation capabilities. Instead, I tend to do this:

package Customer;

use Class::BuildMethods qw/_age/;

sub new { bless $any_reference_you_want, shift }

sub age {
    my $self = shift;
    return $self->_age unless @_;
    my $age = shift;
    die "Too young" if $age < 21;
    $self->_age($age);
}

In the above example, I really should use the validation callback because I'm needlessly duplicating code. But if things are really complicated, I can use this technique of wrapping private methods in a public method to give myself fine-grained control.


Re:Class BuildMethods

DAxelrod on 2006-10-27T01:28:39

I wish the best for your friend, and admire your empathy for them. If humanity has any hope, it lies in such empathy.

I wonder if your uwillingness to use the validate interface indicates that the interface itself may need some tweaking. Is there a way to install the validator such that it can be more naturally extended if neccessary? (My first thought would be to install it as _age_validate or something, so that you could both call it and extend it, but I'm sure you can think of something more elegant.)