[Chicago-talk] A question of style: Class::Accessor

Steven Lembark lembark at wrkhors.com
Mon Nov 24 13:34:41 CST 2003



-- Jim Thomason <jthomasoniii at yahoo.com>

>> Other reason: you're inside the method itself and do
>> not
>> have sufficient stack for infinite recursion. At
>
> True, but ideally, you still want that internal
> structure to be defined and referenced in as few
> places as possible. Only times I've run out of space
> on the callstack is when I've got an error.

For example, the method

	sub foo
	{
		my $item = shift;

		if( ref $item && $item->foo eq 'bar' )
		{
			...
		}
		else
		{
			...
		}

	}

won't get all that much done for given referent.

My only point was that at some point something
will have to access the $item's internals via
something like $item->{foo}, etc, and that
micro-managing performance is not the only reason
to access a data structure directly.

> I disagree. I mean, it's a major pain in the ass to
> have to write all of this stuff, but it's a necessary
> evil. Besides, things like $obj->keys could be easily
> done by
>
> sub keys {
>   my $self = shift;
>   return keys %$self;
> };
> Sure, in that keys method you're referencing the
> internal hash structure, but you're still only
> referencing it in this one place, as opposed to every
> other location that you'd potentially do "keys %$obj".
>
> And heck, it's still perl. :) My preferred root class
> has a method "add_attr" that simply adds an attribute.
> So the start of a class has an add_attr call with an
> attribute, and that automagically creates the
> accessor/mutator.
>
> __PACKAGE__->add_attr('foo');
># I now have $obj->foo() and $obj->foo('new value');
>
> Reduces a lot of the work, you get the benefits of
> encapsulation, and your attributes are pre-declared as
> well.
>
> Though, admittedly, something like ->keys would still
> be a special case. :*)

Which is why I suggested using a standard class-generator
for the object. The problem is that by the time you're
done re-writing keys, all of the named elements, read,
write, iterators, string interpolation, numeric to string
conversion, string to numeric conversion, boolean to string
conversion, string to boolean conversion, existential and
defined operators... the resulting bulk is more work to
manage than a few isolated cases of %$item.

Now nest it a few [dozen] times. And try to debug why it
acts flakey one time in 20 :-)

Jay was originally using a class-class to handle the
accessors, which does make sense if it supports the level
of performance he needs.

--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508



More information about the Chicago-talk mailing list