more useful options | |
PerlMonks |
Class::SelfMethodsby John M. Dlugosz (Monsignor) |
on Sep 15, 2003 at 20:00 UTC ( [id://291640]=perlmeditation: print w/replies, xml ) | Need Help?? |
I was looking at Class::SelfMethods as a possible module to use in a project. I would like to share my thoughts and musings on it. First of all, I think it's very beautiful and simple. It apparently was written by someone who had also written a more straightforward Self-like programming model for Perl, and he was familiar with others. But this design took a step back and meant to offer the interesting charcteristcs that Self-programming brings to the table but integrated nicely with the Perl way of doing things. In that, it does indeed. Basically, you can write a class in the normal way, inheriting from other classes and whatever. But you can include this mechanism to autogenerate accessors that can call a normal package-based method or an overridden attribute or per-instance code override. The code is very small, and it's easy enough to understand so the lack of enlightenment in the docs is not a big issue. The docs explain things ok I guess, but leave me wondering about the right way to apply it. Specifically, what about setting attributes? My impression from the docs is that the _SET suffixed function is for setting up a new per-instance slot. I thought of the slot itself as distinct from the current value within that slot. So, consider a method that returns the current value or sets a new value: Although not addressed, a member that alters the state (like name above, or with separatly-named functions) would certainly be callable and Selfable using the SelfMethod mechanism, though he always speaks of accessors never mutators. But, the confusing part is that he seems to like the _SET suffixed function for setting values as well as slots. Consider the member name("Smith") above will call the code to operate on the parameter. The method can check that the value "Smith" is legal, massage it, update other dependant fields, clear cached data, and whatever else. That's why we like mutators instead of reaching in and altering data fields directly. But, $x->name_SET("Smith") will not call that mutator code, but will in fact replace it in this instance with a constant. That is, using the _SET feature for values as opposed to new slot behavior is exactly like direct access to field data, only worse because it will bypass and dispose the existing mutator. It also defeats any error checking. If I access something that doesn't exist, such as $x->nane, this gives an error. But $x->nane_SET("Smith") will not. This is the same problem as for un-declared globals without using strict, or direct field access via a hash. So, I think that it should generate methods that read and write, for the hash-field case. That is, if there is a field called name then SelfMethods will arrange that calling $x->name() will return $$x{name}, and calling $x->name_SET("Smith") will set $${name}="Smith". I would like to not use the _SET form for changing the value, but have the SelfMethods</code> support $x->name("Smith"). Right now that statement will still fetch the field, ignore the parameter, and give no warning. (whether it overloads the same function for get/set or has distinct names for each is not the issue. Point is, auto-generate a mutator for field-based properties that is distinct from changing the slot itself.) Another thing SelfMethods does is use an underscore prefix for package-based methods. That is, you write a sub _foo that will be called if $x->foo has no per-instance override. This makes sence, and is a minimal solution. My issue isn't with the semantics, but rather that we already use names beginning with an underscore for private methods. I think a different sort of decoration is in order for this new purpose. —John
Back to
Meditations
|
|