Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Complex Objects

by PatMurty (Initiate)
on Dec 03, 2004 at 16:01 UTC ( [id://412163]=perlquestion: print w/replies, xml ) Need Help??

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

Oh wise ones:

Can someone point me to an example of how to implement and access the kind of object shown below?
Most of the examples if found are an object that contains one other object.
I need to create an object that contains a hash of sub-objects each sub-object is made up of a number of different objects and an array of an object type.

Object1 contains scalars
Object2 Contains scalars
Object3 contains scalars (OK that part is easy)
Object4 contains an Object1, an Object2, an array of Object3s, and scalars
Object5 contains a hash of Object4s and scalars
Object6 contains a hash of Object5s

In general the program will need to get or set each element in each object individually.

The ability to enumerate the object3s in the Object6 and the ability to Enumerate through the object4s an object5 are the only other complex accesses I need.

Thanks,
-Pat

Replies are listed 'Best First'.
Re: Complex Objects
by dragonchild (Archbishop) on Dec 03, 2004 at 16:04 UTC
    What problems are you running into? What kind of solutions have you tried and how have they failed? What kind of roadblocks are you running into?

    In other words, we will help you do your (home)work, but we won't do it for you.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      My goal in attempting to use objects with perl may be unreachable.
      What I wanted to do is re-write some code that contains numerous hashes of hashes to use an object structure that encompesses the whole data structure and allows the user to populate and read the values as slices. I also would include a few methods that allowed me to loop through items based on a few rules

      In other languages I would be able to represent a complex object such as:
      obj.fig(x).method1
      obj.fig(x).item(y).r02.someScalar
      obj.fig(x).item(y).r02.methodx
      obj.fig(y).item(z).r05(a).someOtherScaler

      Having looked through a large number of tutorials and other documents. I keep seeing objects that inherit by adding more methods and scalars at the first level. They are populated at creation by anonymous hashes or arrays.
      I don't see examples of objects that allow implement depth AND show a clear example how to populate and read the data using a "slice" type method.

      In my first post I was really asking someone to point me to such an example. I wasn't asking anyone to write my code for me.
      I have successfully used single level objects as shown below in X1 and X2 creating hashes containing multiple objects. What I don't see how to do is create a Y1 object. that contains a X1 and an Array of X2 objects.

      Thanks for your patience,
      Pat
      package X1; use vars qw($AUTOLOAD); my %fields = ( TYPE => undef, CUST => undef, SEQ => undef, ); sub new { my $that = shift; my $class = ref($that) || $that; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; } sub AUTOLOAD { my $self = shift; my $type = ref($self) or warn "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { warn "Can't access `$name' field in class $type"; } if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } } package X2; use vars qw($AUTOLOAD); my %fields = ( DEF => undef, TEXT => undef, QTY => undef, ); sub new { my $that = shift; my $class = ref($that) || $that; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; } sub AUTOLOAD { my $self = shift; my $type = ref($self) or warn "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { warn "Can't access `$name' field in class $type"; } if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } } package Y1; use X1; use Y1; my %fields = ( x1 => new X1, x2 => new X2, # This needs to be an array of X2's dt => undef; }; sub new { my $that = shift; my $class = ref($that) || $that; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; } sub AUTOLOAD { my $self = shift; my $type = ref($self) or warn "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { warn "Can't access `$name' field in class $type"; } if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } } 1;
        What I don't see how to do is create a Y1 object. that contains a X1 and an Array of X2 objects.

        Weak containment with pointers/references is the standard practice, using scalars for single objects and arrayrefs for multiple objects. Roughly for instance:

        package Y1; sub new { ... $self->{X1} = X1->new(); $self->{MANY_X2} = [ X2->new(), X2->new() ]; ... } package main; my $obj = Y1->new(); print $obj->{X1}->{X1prop}; print $obj->{MANY_X2}->[1]->{X2prop};

        --Solo

        --
        You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
Re: Complex Objects
by ikegami (Patriarch) on Dec 03, 2004 at 16:29 UTC

    If you make an object from a hash ref (bless({}, $class)), you can treat the object like any other hash ref.

    If you make an object from an array ref (bless([], $class)), treat the object like any other array ref.

    Refer to perlobj, perldata and perlref, and come back with an attempt if you can't make it work.

Re: Complex Objects
by Zed_Lopez (Chaplain) on Dec 03, 2004 at 16:33 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found