Class::Std and AUTOMETHOD

Discussion in 'Perl Misc' started by don.hosek@gmail.com, Oct 9, 2006.

  1. Guest

    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?
    , Oct 9, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page