Complex Objects in Perl

Discussion in 'Perl' started by John Smith, Dec 3, 2004.

  1. John Smith

    John Smith Guest

    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 Object3, and scalars

    Object5 contains a hash of Object4 and scalars

    Object6 contains a hash of Object5

    In general I will 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
    John Smith, Dec 3, 2004
    #1
    1. Advertising

  2. John Smith

    Jim Gibson Guest

    In article <>, John Smith
    <> wrote:

    > 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 Object3, and scalars
    >
    > Object5 contains a hash of Object4 and scalars
    >
    > Object6 contains a hash of Object5
    >
    > In general I will 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.


    Objects in Perl are implemented as a reference to an anonymous hash.
    The keys to the hash are the names of the members, and the values of
    the hash may be any scalar. This includes normal scalar values and
    references. Thus, a member of your object may be a reference to an
    array or a hash or it may be another object (reference to a hash) or a
    method (reference to subroutine). So you can have any level of nesting
    of objects-within-objects and arrays or hashes of other objects. You
    enumerate over array and hash members as you would enumerate over any
    array or hash given a reference to one of these.

    See the following documentation built into your installation of perl:

    'perldoc perlreftut' Perl references tutorial
    'perldoc perldsc' Perl data structures cookbook
    'perldoc perllol' Manipulating arrays of arrays in Perl
    'perldoc perlboot' Beginner's object-oriented tutorial

    FYI: this newsgroup is defunct; try comp.lang.perl.misc in the future.
    Jim Gibson, Dec 3, 2004
    #2
    1. Advertising

  3. John Smith

    packat Guest

    Jim Gibson wrote:
    > In article <>, John
    > Smith
    > <> wrote:
    >
    > Objects in Perl are implemented as a reference to an
    > anonymous hash.
    > The keys to the hash are the names of the members, and the
    > values of
    > the hash may be any scalar. This includes normal scalar
    > values and
    > references. Thus, a member of your object may be a
    > reference to an
    > array or a hash or it may be another object (reference to
    > a hash) or a
    > method (reference to subroutine). So you can have any
    > level of nesting
    > of objects-within-objects and arrays or hashes of other
    > objects. You
    > enumerate over array and hash members as you would
    > enumerate over any
    > array or hash given a reference to one of these.
    >
    > See the following documentation built into your
    > installation of perl:
    >
    > 'perldoc perlreftut' Perl references tutorial
    > 'perldoc perldsc' Perl data structures cookbook
    > 'perldoc perllol' Manipulating arrays of arrays in
    > Perl
    > 'perldoc perlboot' Beginner's object-oriented
    > tutorial
    >


    Thanks for the info. I have been using perl on and off but
    never tried its OO feature.
    I followed the example in perlcod perlboot but got an error.

    Here is my code
    --------------------

    Cow->speak;

    { package Animal;
    sub speak {
    my $class = shift;
    print "a $class goes ",$class->sound,"!\n";
    }
    }

    { package Cow;
    use vars qw(@ISA);
    @ISA = qw(Animal);
    print "Hi >$ISA[0]>\n";
    sub sound {"Mooo"}
    }
    ---------------------
    After execution, I got

    C:\Perl\MDGS>
    C:\Perl\MDGS>
    C:\Perl\MDGS>Sample.pl
    Can't locate object method "speak" via package "Cow" at
    C:\Perl\MDGS\Sample.pl line 2.

    --------------------

    Does this mean perl can't recgnize @ISA?

    Thanks,
    pac
    packat, Mar 19, 2005
    #3
  4. "packat" <> schreef in bericht
    news:NPY_d.24692$hA3.5491@trnddc09...
    > Jim Gibson wrote:
    >> In article <>, John Smith
    >> <> wrote:
    >>
    >> Objects in Perl are implemented as a reference to an anonymous hash.
    >> The keys to the hash are the names of the members, and the values of
    >> the hash may be any scalar. This includes normal scalar values and
    >> references. Thus, a member of your object may be a reference to an
    >> array or a hash or it may be another object (reference to a hash) or a
    >> method (reference to subroutine). So you can have any level of nesting
    >> of objects-within-objects and arrays or hashes of other objects. You
    >> enumerate over array and hash members as you would enumerate over any
    >> array or hash given a reference to one of these.
    >>
    >> See the following documentation built into your installation of perl:
    >>
    >> 'perldoc perlreftut' Perl references tutorial
    >> 'perldoc perldsc' Perl data structures cookbook
    >> 'perldoc perllol' Manipulating arrays of arrays in Perl
    >> 'perldoc perlboot' Beginner's object-oriented tutorial
    >>

    >
    > Thanks for the info. I have been using perl on and off but never tried
    > its OO feature.
    > I followed the example in perlcod perlboot but got an error.
    >
    > Here is my code
    > --------------------
    >
    > Cow->speak;


    Which Cow, which speak?

    >
    > { package Animal;
    > sub speak {
    > my $class = shift;
    > print "a $class goes ",$class->sound,"!\n";
    > }
    > }
    >
    > { package Cow;
    > use vars qw(@ISA);
    > @ISA = qw(Animal);
    > print "Hi >$ISA[0]>\n";
    > sub sound {"Mooo"}
    > }
    > ---------------------
    > After execution, I got
    >
    > C:\Perl\MDGS>
    > C:\Perl\MDGS>
    > C:\Perl\MDGS>Sample.pl
    > Can't locate object method "speak" via package "Cow" at
    > C:\Perl\MDGS\Sample.pl line 2.
    >
    > --------------------
    >
    > Does this mean perl can't recgnize @ISA?
    >
    > Thanks,
    > pac
    >


    Move your speaking cow to the point beyond the package declarations and she
    will no longer be silent.

    Theo van den Heuvel
    Theo van den Heuvel, Mar 20, 2005
    #4
  5. John Smith

    packat Guest

    Theo van den Heuvel wrote:
    > "packat" <> schreef in bericht
    > news:NPY_d.24692$hA3.5491@trnddc09...
    >> Jim Gibson wrote:
    >>> In article <>, John
    >>> Smith
    >>> <> wrote:

    >> Here is my code
    >> --------------------
    >>
    >> Cow->speak;

    >
    > Which Cow, which speak?
    >
    >>
    >> { package Animal;
    >> sub speak {
    >> my $class = shift;
    >> print "a $class goes ",$class->sound,"!\n";
    >> }
    >> }
    >>
    >> { package Cow;
    >> use vars qw(@ISA);
    >> @ISA = qw(Animal);
    >> print "Hi >$ISA[0]>\n";
    >> sub sound {"Mooo"}
    >> }
    >> ---------------------
    >> After execution, I got
    >>
    >> C:\Perl\MDGS>
    >> C:\Perl\MDGS>
    >> C:\Perl\MDGS>Sample.pl
    >> Can't locate object method "speak" via package "Cow" at
    >> C:\Perl\MDGS\Sample.pl line 2.
    >>
    >> --------------------
    >>
    >> Does this mean perl can't recgnize @ISA?
    >>
    >> Thanks,
    >> pac
    >>

    >
    > Move your speaking cow to the point beyond the package
    > declarations
    > and she will no longer be silent.
    >


    {package packat;
    sub sound {"Duh!"}
    }

    I just follow the sample code in perldoc without reordering
    the sequesce....
    It works fine now. Hmmm.. I have a great projct for it.

    Thnaks for you help,
    pac
    packar->
    Duh!

    > Theo van den Heuvel
    packat, Mar 20, 2005
    #5
  6. John Smith

    Isaac To Guest

    >>>>> "packat" == packat <> writes:

    packat> Cow-> speak;
    packat> { package Animal;
    packat> sub speak {
    packat> my $class = shift;
    packat> print "a $class goes ", $class->sound, "!\n";
    packat> }
    packat> }
    packat> { package Cow;
    packat> use vars qw(@ISA);
    packat> @ISA = qw(Animal);
    packat> print "Hi >$ISA[0]>\n";
    packat> sub sound {"Mooo"}
    packat> }

    packat> After execution, I got
    packat> Sample.pl Can't locate object method "speak" via package
    packat> "Cow" at C:\Perl\MDGS\Sample.pl line 2.

    packat> Does this mean perl can't recgnize @ISA?

    It means you've put the "Cow->speak" too early, before Perl execute
    the statements that assigns @Cow::ISA. Try putting it into the last
    statement, and remember that Perl execute each file in your program
    line by line (except that subroutines are defined before anything is
    executed, together with the things you put in BEGIN {}, includes by
    "use", etc).

    Regards,
    Isaac.
    Isaac To, Mar 20, 2005
    #6
    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. news.amnet.net.au
    Replies:
    1
    Views:
    559
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Vic Russell
    Replies:
    0
    Views:
    438
    Vic Russell
    Sep 29, 2004
  3. Stanimir Stamenkov
    Replies:
    2
    Views:
    734
    Stanimir Stamenkov
    Oct 25, 2005
  4. Robert Mark Bram
    Replies:
    0
    Views:
    673
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    739
Loading...

Share This Page