Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hierarchical structures with objects

Reply
Thread Tools

Hierarchical structures with objects

 
 
Gerry Grieve
Guest
Posts: n/a
 
      09-12-2003

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(0xe4c9

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};
}




 
Reply With Quote
 
 
 
 
Brian Harnish
Guest
Posts: n/a
 
      09-12-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 12 Sep 2003 20:30:00 +0000, Gerry Grieve wrote:
> 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:umper, 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-----

 
Reply With Quote
 
 
 
 
James E Keenan
Guest
Posts: n/a
 
      09-12-2003

"Gerry Grieve" <> wrote in message
news:bjtac8$r6$...
>
> 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:umper 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. 9
%$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


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-12-2003
>>>>> "JEK" == James E Keenan <> writes:

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

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parallel class structures for AST-based objects Steve Howell Python 8 11-22-2009 05:27 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
ASP.NET 2.0 - How to databind to nested (hierarchical) business objects? style ASP .Net Web Controls 2 09-15-2006 10:10 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments