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

Jim Thomason jthomasoniii at yahoo.com
Mon Nov 24 08:37:00 CST 2003

> Other reason: you're inside the method itself and do
> not
> have sufficient stack for infinite recursion. At
> some point
> SOMEthing has to know what the internal format of
> the data
> is (even if it's only perlguts knowing how ->{key}
> works :-).

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. 

> Another reason is that you need to access
> functionality in
> the object not provided by the interface. In theory
> you could
> sit down and write an accessor for everything you
> need to
> perform on the referent, but at that point you may
> have
> simply re-written perl itself. Example is using
> "keys %$obj"
> to sanity check a list. By the time I've written
> $obj->keys
> to extract them it may be simpler to just use what
> Perl hath
> wrought and be done with it -- or use an external
> class to
> buid the objec out of in the first place.

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

#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

Though, admittedly, something like ->keys would still
be a special case. :*)


Do you Yahoo!?
Free Pop-Up Blocker - Get it now

More information about the Chicago-talk mailing list