In general it's fine to store one object inside another as you are doing. However, there is an issue with stuff like this: Quote:
Inside Object2: sub setsomething { my $self = shift; $self->{_object1}->{mode} = 1; }
What this means is that you have given Object2 some "inside knowledge" of how Object1 is implemented, because Object2 is "reaching inside" Object1's hash to get the "mode" key. This is considered bad. You, or someone else, might decide to change the implementation of Object1 -- maybe you'll decide to call "mode" something else, or even that Object1 should use an arrayref instead of a hashref for performance reasons. Then Object2 will break. Clean object encapsulation requires that you hide implementation details from other objects, and present only an API for them to use. What this means is you have to provide an accessor function in Object1 like this:
sub mode { my $self = shift; $self->{mode} = $_[0] if (@_); $self->{mode}; }
And then in Object2, instead of
$self->{_object1}->{mode} = 1;
you use
$self->{_object1}->mode(1);
The main advantages: a) You are free to change the internal implementation of Object1 without affecting Object2. b) You can choose to perform additional tasks in Object1 when mode is set, or read. Even choose to deny access. Writing accessor functions by hand is tedious though, so it's good to use something like Class::Accessor, or weave some magic with AUTOLOAD and a _permitted list.

In reply to Re: Conventions with Passing Objects to Objects by Anonymous Monk
in thread Conventions with Passing Objects to Objects by zxViperxz

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.