Hierarchical structures with objects

Discussion in 'Perl Misc' started by Gerry Grieve, Sep 12, 2003.

  1. Gerry Grieve

    Gerry Grieve Guest

    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};
    }
     
    Gerry Grieve, Sep 12, 2003
    #1
    1. Advertising

  2. -----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::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-----
     
    Brian Harnish, Sep 12, 2003
    #2
    1. Advertising

  3. "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::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
     
    James E Keenan, Sep 12, 2003
    #3
  4. Gerry Grieve

    Uri Guttman Guest

    damian classes (was Re: Hierarchical structures with objects)

    >>>>> "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
     
    Uri Guttman, Sep 12, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,814
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    740
    Emmanuel Delahaye
    Sep 24, 2005
  3. 7stud
    Replies:
    11
    Views:
    725
    Dennis Lee Bieber
    Mar 20, 2007
  4. Steve Howell
    Replies:
    8
    Views:
    346
    Steve Howell
    Nov 22, 2009
  5. style
    Replies:
    2
    Views:
    305
    style
    Sep 15, 2006
Loading...

Share This Page