Complex Objects in Perl

J

John Smith

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
 
J

Jim Gibson

John Smith said:
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.
 
P

packat

Jim said:
Smith

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
 
T

Theo van den Heuvel

packat said:
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

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
 
P

packat

Theo said:
packat said:
Here is my code

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!
 
I

Isaac To

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top