Hierarchical structures with objects

G

Gerry Grieve

I have some data (course Info) which I'm trying to model as an
Object "panda_course" which stores the data into a hash. Besides other things,
a "panda_course" can have one or more "sections" which are modelled as another
Object & the references are stored in an array @ { $panda_course->{sections}.
This part works;

Each section can include one or more "Meetings" which is again an object which uses
an hash to store the info. I wanted to store references to these "meetings" objects
as an array in a "section->{meetings} (the meeting method is below.)
I expected each section to have a unique array of meeting references, but
I get only one array being used.
My test case is 1 course w 3 sections each having 1 meeting.
Below is some debug statements that the meeting method prints out:
Each panda_course_section is an unique hash, but each meeting array
is the same.

what I am missing, (besides a clue !!)

Gve

debug Output

panda_course:section:meeting Put panda_course_meeting=HASH(0xe2d40) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0x6a25c)

panda_course:section:meeting Put panda_course_meeting=HASH(0xe2ee4) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0xe4c98)

panda_course:section:meeting Put panda_course_meeting=HASH(0xe2f5c) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0xe4e3c)

End_of_debug Output

sub meeting
{
my $self = shift;
my $type = ref($self) || die "<<$self>> is not an object\n";
my $rest = shift;
my $m = panda_course_meeting->new();

while ($rest =~ m[<(\w+?)>(.*?)</\1>]msg)
{
next unless ($1);
my $field = $1;
my $value = $2;
$value =~ s/^\s*$//;
$m->$field($value);
}
print "panda_course:section:meeting Put $m on array self{meetings} \n";
print "panda_course:section:meeting The array ref is \$self{meetings} is $self->{meetings} \n";
print "panda_course:section:meeting The self is $self \n\n";
push @ { $self->{meetings} }, $m;

return $self->{meetings};
}
 
B

Brian Harnish

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

what I am missing, (besides a clue !!)

Um, a questionmark?

Also, the rest of your code! How are we supposed to be able to tell whats
in $self->{meetings} when we don't see where it's created, or even used?

Also Also, try using Data::Dumper, much easier to read, and more detail
than your dump.

- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/YjWviK/rA3tCpFYRAoPPAKCLrPjTpu7vi4piNeDiadFN3a1OSQCgnbHn
JmYkLeXhwjsmTsV0YTfmw6M=
=YCts
-----END PGP SIGNATURE-----
 
J

James E Keenan

Gerry Grieve said:
I have some data (course Info) which I'm trying to model as an
Object "panda_course" which stores the data into a hash. Besides other things,
a "panda_course" can have one or more "sections" which are modelled as another
Object & the references are stored in an array @ { $panda_course->{sections}.
This part works;
The points which Brian Harnish has already made are well taken. You need to
show us a little of your input data for us to see why your object is not
being constructed properly, and you should use Data::Dumper to get a picture
of the object.

But I would like to know why you are attempting to construct a complicated
(IMHO) object hierarchy when you could get away with constructing a single
object which blesses a multi-dimensional hash into a class. I recently
faced a similar problem: Modelling a weekly schedule of treatment groups in
a hospital setting. I construct just one object where each group (analogous
to your panda_course) can have multiple sessions within a week. I follow
the practice of separating construction of the object (new()) from
initialization (_init()) as advocated by Damian Conway in "Object-Oriented
Perl." I've always found this very straightforward and wonder what the
advantages of constructing a hierarchy of objects would be.

The code for such an approach would look roughly like this:

sub new {
my ($class, $source, $self, $dataref);
($class, $source) = @_;

# bless a ref to an empty hash into the invoking class
$self = bless {}, ref($class) || $class;

# prepare the database by using &_init
$dataref = _init($source);

# initialize the object from the prepared values (Damian, p. 98)
%$self = %$dataref;
return $self;
}

sub _init {
my $source = shift;
my (%data);

open(IN, $source) || die "cannot open $source for reading: $!";
while (<IN>) {

# parse the data here; store in %data

close(IN) || die "cannot close $source: $!";
return \%data;
}

Jim Keenan
 
U

Uri Guttman

JEK> I follow the practice of separating construction of the object
JEK> (new()) from initialization (_init()) as advocated by Damian
JEK> Conway in "Object-Oriented Perl." I've always found this very
JEK> straightforward and wonder what the advantages of constructing a
JEK> hierarchy of objects would be.

and if you want to learn more about OO Perl, modules and regexes from
damian conway, you can take classes with with him in boston on sept 29 -
oct 2.

http://www.stemsystems.com/class

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top