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

Steven Lembark lembark at wrkhors.com
Mon Nov 24 00:24:54 CST 2003


> There is only one reason to not use the accessor methods, and that's
> because for some reason you're microoptimizing your code for speed and
> you can't afford the function calls.  If you've gone ahead and profiled
> your code, say, with Devel::DProf, and you know that calls to your
> accessors are causing a slowdown, and you've eliminated other sources of
> speedup, and you comment your code explaining WHY you're using the
> attributes directly, then sure, go ahead.

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 :-).

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.

Aside: Perl does a rather nice job of caching method names,
so there isn't all that much execution time savings unless
you are in really, really tight loops.

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



More information about the Chicago-talk mailing list