Class::Std and AUTOMETHOD

D

don.hosek

OK, so I'm going back over the last project to see what I can learn
from my efforts and I run into one annoying bit of cut-and-paste coding
which would better off not being such.

I had a number of classes derived from a base class which had some
member data which were in array form:

package A;
use Class::Std;
use base qw(BaseClass);
use strict;

# Both of these will be references to arrays
my %this_list_ref_of : ATTR;
my %that_list_ref_of : ATTR;

I want to have methods which give me the number of elements so that I
can do

my $foo=A->new();
print $foo->count_of_this_list_ref_of();

Now generating the functions by cut and paste is doable, but it's also
error-prone: What if I accidentally don't do all the substitutions and
$foo->count_of_that_ref_of() gives me the size of the array in
$this_list_ref_of{$this} rather than $that_list_ref_of{$this}?

Ignoring the autovivification issues, I had tried setting up an
AUTOMETHOD which used symbolic refs and found that this wasn't going to
work [easily] since an auto-vivified ${'foo'} is a different entry in
the symbol table than my $foo. I suppose I could look at what class my
AUTHOMETHOD is called from and use that to set up the symbol table, but
there's still the whole autovivification issue. Any thoughts on what
the best practice way of dealing with this problem would be?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top