Over the last few months I have set about converting one of my largest projects into Perl Objects and have been thrilled by the functionality perl's objects provide (not to mention the performance boost in mod_perl).

My question however relates to passing objects to other objects. So far I've been creating objects then passing them to other objects in the constructors e.g.
my $object1 = new MyClasses::Object1 ( _cgi => new CGI, param1 => '...', param2 => '...', ); $object1->dothis; my $object2 = new MyClasses::Object2 ( _object1 => $object1, param1 => '...', param2 => '...' ); $object2->setsomething; my $object3 = new MyClasses::Object3 ( _object1 => $object1 ); $object3->blah;
The way this works I can declare all my parameters for respective objects and reuse them again without wasting new declarations, and modify them between different objects, e.g.
Inside Object2: sub setsomething { my $self = shift; $self->{_object1}->{mode} = 1; } Inside Object3: sub blah { my $self = shift; if ($self->{_object1}->{mode}){ return $self->{_object1}->{_cgi}->param('blah'); } else { return $self->{_object1}->{_cgi}->param('pleh'); } }
After looking at a bunch of modules and other perl code I have realised I have made an error by making object argument names _object seeing that is is against the private variable declaration (however I really like my convention seeing that I can tell an object and a normal argument apart).

I have also been unable to find any other code that uses object passing like this which brings be to my question:

How, as a proper perl programmer using proper conventions should this be written? I have not seen any object passing in other CPAN modules or example code so how should it be done?

I realise that what I have done works and for me works very well but knowing the correct way to do it is very important for me for future reference (or rewriting if I've completly stuffed it up) =).

Thanks,
James.

P.S. Hope you can understand what I'm trying to ask, It's very early and I'm almost out of battery =)

In reply to 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.