Re: Sharing instances of objects between packages

From: R. Joseph Newton (rjnewton_at_efn.org)
Date: 01/06/04

  • Next message: Chris: "(I wish there was a) cpan user's guide"
    Date: Mon, 05 Jan 2004 22:19:40 -0800
    To: Shawn McKinley <sm_beginners_perl_org@baimc.com>
    
    

    Shawn McKinley wrote:

    > Hello all,
    > I am wondering if you can have object inherited between
    > packages when the child packages have their own object
    > creation without explicitly setting the parent object in
    > the child? Is there a way to inherit the parent object?
    > Example below (sorry for the length).
    >
    > TIA,
    > Shawn

    Hi Shaun,

    What is it you are trying to model? Generally, it is not a good
    idea to make objects interdependent more than absolutley
    necessary. I'm a little unclear about what you are getting at. It
    isn't a good idea to try to access data members of a parent object,
    because there should not be a separate parent in inheritance
    relationships. Say you have a class Building

    package Building;

    use strict;
    ...

    sub initialize {
       my $self = shift;
       $self->{'footprint width'} = shift;
       $self->{'footprint length'}= shift;
       ... other generic building stuff
    }

    sub footprint_width {
       my $self = shift;

       return $self->{'footprint width'};
    }

    ...and you decide to subclass House from building. You have access
    to the methods of the parent class, so you can call them without
    having to redefine them. On the other hand, you should not think
    of the data as being part of any other object. The relationship is
    an "izza" relationship. The House "is a" Building. There is no
    separate Building to have the House's 'footprint width' member.

    package House;

    use strict;
    ...

    use Building;

    use Exporter;

    my @ISA = 'Building';

    sub new {
       my $class = shift;
       ...yada, yada
       $self->initialize(@_);
    }

    sub initialize {
       my $self = shift;

       $self->{'footprint width'} = shift; # just as with the Building
    initializer
       ...
    }

    Note that this data value is assigned to the object of the
    immediate class without reference to the parent class. The thing
    is, the footprint_width method of the parent can be called on this
    object, and will get the member of the House object.

    Inheritance is basically about inheriting those metohds. Use it
    sparingly, and aim for transparency.

    Joseph


  • Next message: Chris: "(I wish there was a) cpan user's guide"

    Relevant Pages

    • Re: trouble with delegating unlock rights
      ... > Effective Permissions on this object are: ... > CONTROL ... > <Inherited from parent> ... inheritance enabled ...
      (microsoft.public.win2000.active_directory)
    • Re: Controlling object visibility
      ... It has AU as a member. ... BTW, I would avoid the deny ace, ... but I'd rather block inheritance at Division level. ... > <Inherited from parent> ...
      (microsoft.public.windows.server.active_directory)
    • Re: instance attributes not inherited?
      ... >> Nothing's wrong with python's oop inheritance, ... >> class Child(Parent): ... >the class name be enough for super() to find the right superclass object? ... a bound method has the first argument bound in, and when you call the bound method ...
      (comp.lang.python)
    • Re: Refactoring Tycho API - Opinions wanted
      ... the "flip parent and child" is a useful op. ... > inheritance, but contained in a parent-child relationship. ... > store each child item (a reference to a sub-topic or note, ...
      (comp.lang.ruby)
    • Re: The Language I want
      ... inheritance of interfaces from concrete types. ... modules and packages. ... Templates or hygenic macros are not generics! ...
      (comp.lang.misc)