Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Complex Objects in Perl

29 views
Skip to first unread message

packat

unread,
Mar 19, 2005, 11:55:09 AM3/19/05
to
Jim Gibson wrote:
> In article <8_ydnS49C86...@adelphia.com>, John
> Smith
> <som...@microsoft.com> 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


Theo van den Heuvel

unread,
Mar 20, 2005, 3:40:19 AM3/20/05
to
"packat" <cha...@verizon.not> schreef in bericht
news:NPY_d.24692$hA3.5491@trnddc09...

> Jim Gibson wrote:
>> In article <8_ydnS49C86...@adelphia.com>, John Smith
>> <som...@microsoft.com> 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


packat

unread,
Mar 20, 2005, 7:15:21 AM3/20/05
to

{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


Isaac To

unread,
Mar 20, 2005, 8:39:29 AM3/20/05
to
>>>>> "packat" == packat <cha...@verizon.not> 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.

0 new messages