On Apr 21, 2010, at 7:30 PM, Kate Yoak wrote:
Hi all!
I want to know more about why class data is the Wrong Thing to do.
I think of class data as complex constants. For example, say, I
have a Model layer, which aggregates ORM table objects. I might
have something like this:
use constant orm_class_name =>'Table::Account';
has table (..., builder => '_orm_table');
...
sub _orm_table{
shift->orm_class_name->new();
}
sub class_method_from_orm{
shift->orm_class_name->method();
}
---
Now it is sensible for me to compute the orm_class_name based on the
current package name - and a builder is the elegant way to do it
(and I have to admit, builders are just plain pretty!)
class_has orm_class => (..., builder => '_orm_class', ...other fancy
stuff possible...);
--
the rest of the code is unchanged - the class data is really little
more than a very pretty constant.
OK, so bottom line is, I am twitching because Dave Rolsky says, I am
a Bad Person, and seeing as he's been right about everything else
thus far, I'd like to see whether my argument makes sense, or if
there is a better way to do it.
So this looks fine to me, and I suspect that what your doing will not
upset The Dave as it is a perfectly reasonable use of class data.
But this is perl and there is always more than one way to do it.
Personally I would just have the builder in _orm_table figure this all
out? or add an instance level 'orm_table' attribute so that I could
vary it on a per-instance basis if the need ever arose.
My own objection to class data (which is why it is not and never will
be in core Moose) is that I think specifying class level data tends to
limit the re-use of the class (in my own experience of course, I am
not making a sweeping generalization about all code). Being able to
vary all associated data on a per-instance basis allows a great degree
of freedom and makes it (IMO again) easier to use a class in a way
that it was not originally intended. Sure, sometimes this means a lot
of duplicated data is floating around in similar objects, but I have
never actually found this to be a problem and if it was I would simply
do a site specific optimization for it (possibly involving class data).
Anyway, just my 2 cents.
- Stevan