Hi Monks,

Please advise on this.

package ABC; { sub new { my $hashref={}; my @array=(); my $arrayref=[]; $class=$_; bless ($hashref,$class); bless (@array,$class); bless ($arrayref,$class); return $hashref, @array, $arrayref; #I think I can only return one of these three data structures but not +all, right? } sub get { #To access $hashref, @array, and $arrayref } sub set { #To set $hashref, @array, and $arrayref } } 1; #main $object1=ABC->new(); #assuming ABC class returns $hashref $object2=ABC->new(); #assuming ABC class returns @array $object3=ABC->new(); #assuming ABC class returns $arrayref

Basically, I want to create a class ABC with three separate data structures, $hashref, @array, and $arrayref and I want some mechanism to manipulate the data in them. Questions are,

Q1. How do I make the constructor return all three data structures, $hashref, @array, and $arrayref when invoked. Is it possible? I understand that only one of these three can be returned by the constructor and not all together. Correct?

Q2. I guess with $object1,$object2,$object3, I am creating three different objects. Right? Is there a way by which I create just one object of ABC and through this object I am able to manipulate data in all three data structures of, $hashref, @array, and $arrayref.

Q3. Do I need to create three different sets of get() and set() functions each to manipulate $hashref, @array, and $arrayref respectively by creating their objects first, ie $object1,$object2,$object3 ?

Q4. I understand I can manipulate $hashref, @array, and $arrayref only through access functions like get() and set() and not directly like $object1->$hashref or something like it, am I correct?

Q5. Suppose I create a nested data structure reference in the constructor, say $all={}, that contains all three, $hashref, @array, and $arrayref and if I bless $all with class ABC, then I can create just one object which I can then use to manipulate the data contained in $hashref, @array, and $arrayref. Also, this way I may just need one set of get()/set(). Is this a feasible solution? Would a nested data structure of $hashref, @array, and $arrayref together be better solution than their three separate data structures? Perhaps, the nested structure might make the program slower, yes/no?

I am not sure if I am making sense. Am I missing something? Please let me have your comments guys. What are the common ways of doing such things? Please guide.

Thanks a lot!

Cheers, Raj


In reply to On Perl Objects! by newbio

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.