http://qs321.pair.com?node_id=59029

archon has asked for the wisdom of the Perl Monks concerning the following question:

basically, i want to instantiate an object of the base class (Foo).then i want to instantiate an object of the subclass (Foo::Bar). i want the subclass object to know about the object data of the superclass object.

for some reason, the following feels dirty to me.. can anyone suggest a better way?:

package Foo; use strict; sub erf { my ($self) = shift; print $self->name(); } sub new { my ($proto, $args) = @_; my $class = ref($proto) || $proto; my $self = {}; if (ref($args) eq 'HASH') { if (exists $args->{'name'}) { $self->{'_name'} = $args->{'name'}; } else { return undef; } } else { $self->{'_name'} = $args->name(); } bless $self, $class; return $self; } sub name { my ($self) = shift; return $self->{'_name'}; } package Foo::Bar; use strict; @Foo::Bar::ISA = qw/Foo/; sub doit { my ($self) = shift; $self->erf(); } package main; use strict; my %hash = (name => 'Homer'); my $foo = Foo->new(\%hash); my $bar = Foo::Bar->new($foo); $bar->doit();

Edit: 2001-03-03 by neshura