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. Advertisements

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.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    5,665
    Thomas Weidenfeller
    Oct 12, 2005
  2. Peter Jansson
    Replies:
    5
    Views:
    6,924
    Ivan Vecerina
    Mar 17, 2005
  3. Vinu
    Replies:
    4
    Views:
    525
    Jim Langston
    Jul 7, 2005
  4. Vinu
    Replies:
    0
    Views:
    479
  5. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,277
    Earl Purple
    Jan 18, 2006
  6. puzzlecracker
    Replies:
    3
    Views:
    2,244
    Mike Wahler
    May 8, 2006
  7. Jeffrey Walton
    Replies:
    10
    Views:
    1,232
    Mathias Gaunard
    Nov 26, 2006
  8. , India
    Replies:
    3
    Views:
    3,457
    James Kanze
    Nov 13, 2010
Loading...