Any alternative suggestion? How is this kind of things generally done?

It's usually cleaner to demote the object's data to a separate class owned by the parent object, or even just to a single data structure owned by the parent object:

Instead of this:

package Parent_object; sub new { my $O = bless {}, shift; $O->{'attribute-1'} = "value 1"; $O->{'attribute-2'} = "value 2"; $O->{'attribute-3'} = "value 3"; [...] return ($O); }

do this:

package Parent_object; sub new { my $O = bless {}, shift; $O->{'data'} = _fill_attributes (@_); return ($O); } sub _fill_attributes { my $result = {}; $result->{'attribute-1'} = $_[0]; $result->{'attribute-2'} = $_[1]; $result->{'attribute-3'} = $_[2]; [....] return ($result); }

By demoting the data, you get rid of the circular dependency between the parent object and the user-supplied child function. The child function doesn't need to know anything at all about the parent object, it only needs to know the data object, or hash, or whatever. Instead of calling your child functions like so:

package Parent_object; [...] sub do_something { my $O = shift; my $f = $O->{'sub-function-for-this-case'}; $f->($O); return ([whatever]); }

You can just pass the data:

package Parent_object; [...] sub do_something { my $O = shift; my $f = $O->{'sub-function-for-this-case'}; $f->($O->{'data'}); return ([whatever]); }

It may not look like that big a change, but it gets rid of chicken-and-the-egg problems that can drive you absolutely nuts at design time.


In reply to Re: Seeking advice for OO-related strategy by mstone
in thread Seeking advice for OO-related strategy by blazar

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.