Hello v_melnik,

I’m having trouble understanding your OO design. In part, I suspect it’s a problem of terminology. For example, you say:

I'm passing the reference to the parent object to the child object...

But in OO there’s no such thing as a “parent object” or a “child object.” There are parent and child classes, related by inheritance, but the inheritance relation does not extend to objects. For example, if you had a parent class Dog and a child class Labrador_Retriever, you could create two objects:

my $rex = Dog->new(name => '$Rex'); my $fido = Labrador_Retriever->new(name => 'Fido');

but it would be wrong to refer to $rex as the “parent” of $fido in this case. Actually, it’s also usually a bad design to allow a parent class like Dog to be instantiated at all. Dog should be an abstract base class.

In Perl, there’s an additional source of confusion in the naming scheme of the module hierarchy. For example, if you use two classes like this:

use Foo; use Foo::Bar;

it seems natural to assume that Foo::Bar is a child class of Foo. But that needn’t be the case; Foo::Bar may be completely unrelated to Foo. The package name Foo::Bar simply tells Perl to search for the Bar package in a directory named Foo. To create an inheritance relationship (in Moose) you would normally use extends:

package Foo::Bar; use Moose; extends 'Foo';

Now to the real issue: inter-object communication. You write:

I'm passing the reference to the parent object to the child object, because I need the child to have access to the parent and its methods & accessors.

Why? If there’s a true inheritance relationship, then a child class object already has access to the methods in the parent class. In all other cases, communication between objects should be via the public interfaces of those objects. That — encapsulation — is a large part of what OO is all about! And if you have to subvert encapsulation to a large extent (as your use of attributes appears to be trying to do), that would tend to indicate a basic flaw in the OO design.

But I apologise if I’ve misunderstood the design issue behind your question. If so, will you please explain why standard, public-interface-based communication between objects is inadequate to your requirements? Also, exactly what problem is the attribute-based design you’ve outlined intended to solve? Try to provide a short, self-contained example showing communication between two or three objects.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Names of attribute accessors (Moose) by Athanasius
in thread Names of attribute accessors (Moose) by v_melnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.