Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Inheriting object data

by archon (Monk)
on Feb 17, 2001 at 02:57 UTC ( #59029=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Inheriting object data
Download Code
Re (tilly) 1: Inheriting object data
by tilly (Archbishop) on Feb 17, 2001 at 03:05 UTC
    Erm, I don't see why you want two objects. Just create an object in class Foo::Bar by calling the new method that it inherits from Foo.

    If you want to, you can actually give Foo::Bar a new() method which does some stuff and is a wrapper around SUPER::new (which is a "fake" method to get at your parent's new method).

      i must be missing something.. i thought i _was_ creating an object in class Foo::Bar by calling the new method that it inherits from Foo. however, that thought seems incongruous with "I don't see why you want two objects." are you saying i don't want to create an object from the base class? i suppose that is an option.

      i am open to any solution that lets me use the subclass with the data that i fed the base class without having to give this data to the base class.

        Just construct an object in Foo::Bar directly without making something in Foo first and pass it the data. That object "is-a" Foo as well.

        In other words my point is that the object you create in the base class Foo just seems to be a redundant object. It isn't used, it isn't necessary. I see no reason to not just lose it and then you have no problem left.

Re: Inheriting object data
by coreolyn (Parson) on Feb 17, 2001 at 03:28 UTC

    You haven't ..instantiate an object of the subclass (Foo::Bar) I'm not seeing a bless in there anywhere. All you have is a function that has access to Foo's method's . (Which you already have in package main).There is no true OO inheritance or subclass in Foo::Bar.


      Erm, I think you misread the code. Foo::Bar inherits a perfectly good new() constructor from Foo, and that constructor uses the 2-argument form of bless correctly. Therefore you do indeed have a constructor for Foo::Bar.

        Aw hell... You know we really need another voting catagory ++, --, & doh! Thanks for pointing out da error's of me ways :)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://59029]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-07-26 00:40 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (175 votes), past polls