Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Array as object property

by fmogavero (Monk)
on May 17, 2001 at 20:39 UTC ( [id://81297]=perlquestion: print w/replies, xml ) Need Help??

fmogavero has asked for the wisdom of the Perl Monks concerning the following question:

I have an object (object1) that creates an object (object2)

Object2 has a property that is on array. This property will store references to new objects (object3).

I want object1 to create any number of object3 and then store the references to the object3's in object2.

How do I load the reference to object3 into the property of object2?

Replies are listed 'Best First'.
Re: Array as object property
by Masem (Monsignor) on May 17, 2001 at 20:48 UTC
    Two ways to do it, but one is much more sane than the other:

    1) Pass a reference of the array in object2 to object1, so it can add it directly.

    2) Have a method in object2 that allows you to add to this array; object1 repeatedly calls this function for each object3 it adds

    Method 2 is much much much better from the object oriented stand point as it hides the details of object2 from other objects. Say in the future, you decide to replace that array in object2 with a hash for speeding up another part of the process. With Method 2, you only need to modify how to add things via that function, while method 1 requires modifying object1's code to handle the change. Not the best design here.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Array as object property
by arturo (Vicar) on May 17, 2001 at 20:53 UTC

    All you need to do is
    (a) store the array of obj3's as an array reference.
    (b) dereference to access them.

    Sample code : in obj2_maker method:

    my @obj3s; while (CONDITION) { push @obj3s, Object3->new(); } my $obj2 = Object2->new(-children=>\@obj3s); # edited ... had the drea +ded "=" without the ">"

    Note you'll have to write an appropriate constructor for Obj2.

    In obj2's children method:

    sub children { my $self = shift; @_ ? $self->{_children} = shift : $self->{_children}; }

    That method will return the anonymous array of children (if nothing is passed to it), or will set the children of the object to the array that is passed to it.

    Access the array by de-referencing. @{ $obj2->children() }

    This is a *very basic* framework. Don't take it as gospel. Just remember 'references' =)

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://81297]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found