in reply to Re: opinions on the best way to inherit class data
in thread opinions on the best way to inherit class data

Thanks for the reply, chromatic.

i think i was perhaps not clear enough about my definition of 'class data', as opposed to 'class defaults', which i believe your response addresses. my apologies.

some typical 'class data' for example might be:

# base class package AbstractMyClass; my $_Class_Count = 0; sub new { my $class = shift; my $this = bless( {}, $class ); ref($this) && $_Class_Count++; return $this; } package MyClass; @MyClass::ISA = ('AbstractMyClass'); sub new { my $class = shift; my $this = $class::SUPER->new( @_ ); # this class' init... return $this; }

if i were to use the code...

use AbstractMyClass; use MyClass; my $amc = new AbstractMyClass (); my $mc = new MyClass ();

...$AbstractMyClass::_Class_Count would be 2, whereas i would have really liked $AbstractMyClass::_Class_Count to be 1, and $MyClass::_Class_Count to be 1. Probably a bad example of the essence of my original question, but nevertheless illustrative of what i am terming 'class data'. thanks, matt aka d_i_r_t_y, since 'zero cool' was taken... ;-)

Replies are listed 'Best First'.
RE: RE: Re: opinions on the best way to inherit class data
by chromatic (Archbishop) on Aug 23, 2000 at 08:33 UTC
    Oh, that's what you mean! Here's a sneaky way to do it:
    #!/usr/bin/perl -w use strict; package Superclass; use vars qw ( $AUTOLOAD ); my $classdata = qq| { my \$self = shift; my \$invocations = 0; sub _increase { \$invocations++; } sub number { return \$invocations; } }|; sub new { my $class = shift; if ($class ne __PACKAGE__) { # he he he eval qq|package $class; $classdata |; } my $self = {}; bless($self, $class); $self->_increase(); return $self; } eval $classdata; package Subclass; use vars qw ( @ISA ); @ISA = qw ( Superclass ); package main; print Superclass->number(); my $one = Superclass->new(); print $one->number(); print Subclass->number(); my $two = Subclass->new(); print $two->number();
    If your superclass is in a different module, you can play around with the import subroutine and do it a bit more cleanly.

    This technique has the unfortunate effect of making Subclass->number() return 1 before you create any instance of Subclass. (Inheritance... oh well.)