B
Ben Jones
Hi,
I'm farily new to oo perl, and wondered if either someone could help me with
the following problem, or suggest a less ugly way of doing it...
I am creating an object whose attributes are loaded from various data
sources. Various groups of attributes are loaded from specific sources,
but I don't want the overhead of loading attributes when client code hasn't
asked for it yet, and might not ever. I didn't want to write an accessor
for each, so I thought I's use AUTOLOAD. My AUTOLOAD method checks to see
if the hash attribute exists, and if it doesn't, calls a private method to
load the correct one. The first solution I thought of to accomplish this
was to use a hash, with the attribute sought being the key, and the value
being a ref to the apporpriate method. So in non oo, something like this:
sub _load_attrs {
my $attr = shift;
my %attr_lookup = ( 'attr1' => \&method1,
'attr2 => \&method2);
}
....etc
But this confuses me in oo - how do I do the refs, and how do I deference
them?
My initial thought would be:
sub _load_attrs {
my ($self, $attr) = @_;
my %attr_lookup = ( 'attr1' => \$self->_load_group1,
'attr2' => \$self->_load_group2);
}
....etc
Is that right? If it is, how do I deference the methods? Or is this
incredibly wrongheaded?
I'm farily new to oo perl, and wondered if either someone could help me with
the following problem, or suggest a less ugly way of doing it...
I am creating an object whose attributes are loaded from various data
sources. Various groups of attributes are loaded from specific sources,
but I don't want the overhead of loading attributes when client code hasn't
asked for it yet, and might not ever. I didn't want to write an accessor
for each, so I thought I's use AUTOLOAD. My AUTOLOAD method checks to see
if the hash attribute exists, and if it doesn't, calls a private method to
load the correct one. The first solution I thought of to accomplish this
was to use a hash, with the attribute sought being the key, and the value
being a ref to the apporpriate method. So in non oo, something like this:
sub _load_attrs {
my $attr = shift;
my %attr_lookup = ( 'attr1' => \&method1,
'attr2 => \&method2);
}
....etc
But this confuses me in oo - how do I do the refs, and how do I deference
them?
My initial thought would be:
sub _load_attrs {
my ($self, $attr) = @_;
my %attr_lookup = ( 'attr1' => \$self->_load_group1,
'attr2' => \$self->_load_group2);
}
....etc
Is that right? If it is, how do I deference the methods? Or is this
incredibly wrongheaded?