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;

In reply to Re^2: Complex Objects by PatMurty
in thread Complex Objects by PatMurty

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.