[Israel.pm] Followup on Gaal's talk on Class::Accessor - inside out classes.

Yosef Meller mellerf at netvision.net.il
Sat Feb 28 12:52:23 PST 2004

Hash: SHA1

Hi all!

First, I really enjoyed the conference, it was very educating and I'm
only sorry I had to leave before the end. Kol Haqkavod to the organizers
~ and lecturers.

In one of the lightning talks, Gaal talked about the usefulness of
accessors and introduced Class::Accessor as a class that takes away most
of the work in dealing with accessors.

There are many good reasons for using accessors. However there was one
reason that was mentioned that I think is *not* one of those. This is
the subject of typos.

In the lecture it was mentioned that using hash keys as an object's
attributes makes it possible for a typo to go unnoticed and a very big
bug to be hard to detect, which is true. The solution sugested was to
use accessors even within your object to avoid the typos.

However, this has a downside of adding the call overhead where you need
a simple assignment. There must be a better way, right? Right.

There's a technique called inside out objects, in which an attribute,
rather than being a key in a hash, is a hash in itself keyed by object
references. Like so:

package MyModule;

my (%name, %colour, %air_speed_of_a_swallow);

sub new {
	return bless {}; # Empty, we only need it for a reference.

sub ask_me {
	my $self = shift;
	return "my name is $name{$self}
		and my colour is $colour{$self}.";

sub DESTROY {  # We must release memory manually
	my $self = shift;
	foreach(\%name, \%colour, \%air_speed_of_a_swallow) {
		delete $_->{$self}

This way we catch typos with the usual 'use strict' and we can still use
~ the attributes directly.

As the perl word goes, there are modules on CPAN similar to
Class::Accessor that work with inside out classes. I just can't
remmember which ones.

Just thought I'd share it with you.

- --
perl -e'$b=unpack"b*",pack"H*","59dfce2d6b1664d3b26cd9969503";

My public key:
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


More information about the Perl mailing list