OO Perl - How to maintain class state when using inherited methods?



Hi,
Here are a couple example class files:

#Collection.pm

package Collection;

our $ClassType = 'Collection_type'; # Class variable

sub new ($;) {
my($class_name) = @_;
my $self = {};
$self->{class_name} = __PACKAGE__;
$self->{property_1} = 'foo';
bless($self, $class_name);
return($self);
}# end new()


sub serializeObject($;) {
my($self) = @_;
my $serialized_object = $ClassType . ',' .
$self->{class_name} . ',' .
$self->{property_1};
return($serialized_object);
}# end serializeObject()


sub getClassType($;) {
return($ClassType);
}# end getClassType()

1;

----------------------------------

#Addresses.pm

package Addresses;

use Collection;

our @ISA = (
'Collection',
);

our $ClassType = 'Addresses_type'; # Class variable.

1;

-------------------------------

Test script:

perl -e 'use Data::Dumper; use Collection; use Addresses;'\
-e 'my $obj_addr = Addresses->new();'\
-e 'print("\n" . Dumper($obj_addr) . "\n" . '\
-e ' "Serialized Object = " . $obj_addr->serializeObject() .
"\n" . '\
-e ' "getClassTtype = " . $obj_addr->getClassType() . "\n"'\
-e ');'

--------------------------------

Output:

$VAR1 = bless( {
'class_name' => 'Collection',
'property_1' => 'foo'
}, 'Addresses' );

Serialized Object = Collection_type,Collection,foo
getClassTtype = Collection_type

---------------------------------------

Questions:

(1) Referencing the dumped object in the above output, I can see that
the namespace of the object is 'Addresses', but all of the internal
properties are that of the parent class Collection.pm. Is it that the
namespace was set to 'Addresses' simply due to the arrow notation
implicitly invoking the parent class' new() method? And the object
really has Collection properties because Perl already walked up the ISA
tree into the Collection namespace?

(2) Both classes have the same class level variable $ClassType. Even
though the namespace of the $obj_addr is 'Addresses', when invoking the
inherited method getClassType(), the output still reflects that of the
parent class' $ClassType? Why? Again, has Perl already walked up the
ISA tree to execute this inherited method, and therefore it sees the
namespace 'Collection' vice 'Addresses'?

(3) If I want to keep Addresses level stateful data inside the object,
is the proper way to do so to pass the data into the inherited
constuctor as arguments?

example:

$obj_addr = Addresses->new($Addresses::ClassType);

Collection.pm: new()
------------
sub new ($;$) {
my($class_name, $stateful_data) = @_;
my $self = {};
$self->{data_type} = $stateful_data;
bless($self, $class_name);
return($self);
}# end new()


I feel like I'm missing some basic premis, that this should be able to
be done without the explicit need to pass the data into the inherited
constructor. Does anyone have any suggestions on how to achieve this
differenlty then my example above?

Thanks in advance for any advice you can offer.

- J.

.



Relevant Pages

  • Re: OO Perl - How to maintain class state when using inherited methods?
    ... sub serializeObject{ ... Referencing the dumped object in the above output, I can see that the namespace of the object is 'Addresses', but all of the internal properties are that of the parent class Collection.pm. ... ISA tree to execute this inherited method, and therefore it sees the namespace 'Collection' vice 'Addresses'? ...
    (comp.lang.perl.misc)
  • Re: Weird bug in VS.NET 2003 IDE
    ... > From time to time, when I save my code, the IDE decides to add lines to ... > End Sub ... > End Namespace ... > the IDE adding all these lines is that the program fails to compile, ...
    (microsoft.public.vsnet.ide)
  • Re: Another small question - closing all forms except one
    ... you need a message loop to process the ... sub main ... dim f as form ... i.e. if you disable the My namespace, the Application Framework does not work anymore. ...
    (microsoft.public.dotnet.languages.vb)
  • use Namespace to reference a form in a Module?
    ... I have added a Namespace to my vb.net project. ... Sub Main ... In a separate module, Module2, I want to reference Form1 ... I could not see txtData in a dropdown after Form1. ...
    (microsoft.public.dotnet.languages.vb)