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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Inheriting object data
by tilly (Archbishop) on Feb 17, 2001 at 03:05 UTC | |
by archon (Monk) on Feb 17, 2001 at 03:12 UTC | |
by tilly (Archbishop) on Feb 17, 2001 at 03:15 UTC | |
by archon (Monk) on Feb 17, 2001 at 03:24 UTC | |
by tilly (Archbishop) on Feb 17, 2001 at 03:33 UTC | |
| |
|
Re: Inheriting object data
by coreolyn (Parson) on Feb 17, 2001 at 03:28 UTC | |
by tilly (Archbishop) on Feb 17, 2001 at 03:37 UTC | |
by coreolyn (Parson) on Feb 17, 2001 at 03:43 UTC |