Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl and Inheritance strangeness.

Reply
Thread Tools

Perl and Inheritance strangeness.

 
 
Anthony Roy
Guest
Posts: n/a
 
      09-09-2004
Hi all,

I am trying to solve a problem to do with inheritance which I just
cannot fathom. Four example files are at the bottom of this email which
I think are a pretty much minimal example of the problem.

I'm basically trying to inherit some methods from a super class. Parent
defines two methods, new and getClass. The file Inheritence.pl basically
creates an instance of each of two subclasses of Parent, printing out
the value returned by the getClass() method after each is created, and
then prints out the value returned by getClass on the first object
again. These are the results:

A: ChildA
B: ChildB
A: ChildB

even though I haven't explicitly changed any values in A. Surely the
first and third lines should be identical? What am I missing here, or am
I just better off avoiding inheritance in perl?

Note I am using ActiveState perl v5.8.1 on a Windows XP box.

Thanks,

Anthony Roy.

-------------------------------------------------------------
#Parent.pm
package Parent;

sub new {
my $class = shift;
my $self = {};

$self{Class} = $class;

bless $self, $class;
}

sub getClass{
my $self = shift;
$self{Class};
}
1;
-------------------------------------------------------------
#ChildA.pm
package ChildA;

use Parent;
@ISA = (Parent);

1;
-------------------------------------------------------------
#ChildB.pm
package ChildB;

use Parent;
@ISA = (Parent);

1;
-------------------------------------------------------------
#Inheritence.pl
use ChildA;
use ChildB;

my $childA = ChildA->new();

print "A: " . $childA->getClass() . "\n";

my $childB = ChildB->new();

print "B: " . $childB->getClass() . "\n";
print "A: " . $childA->getClass() . "\n";



 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-09-2004
Anthony Roy <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi all,
>
> I am trying to solve a problem to do with inheritance which I just
> cannot fathom. Four example files are at the bottom of this email which
> I think are a pretty much minimal example of the problem.
>
> I'm basically trying to inherit some methods from a super class. Parent
> defines two methods, new and getClass. The file Inheritence.pl basically
> creates an instance of each of two subclasses of Parent, printing out
> the value returned by the getClass() method after each is created, and
> then prints out the value returned by getClass on the first object
> again. These are the results:
>
> A: ChildA
> B: ChildB
> A: ChildB
>
> even though I haven't explicitly changed any values in A. Surely the
> first and third lines should be identical? What am I missing here, or am
> I just better off avoiding inheritance in perl?


You're better off using strict (and warnings). Introduce them in
your code and try again. Also, use Data:umper to see what your
objects actually contain.

See the problem now?

Inheritance in Perl does have its issues, but on the level you're
acting here it works just fine. Your code is in error, in several
places. Strict will point them out.

Anno

> -------------------------------------------------------------
> #Parent.pm
> package Parent;
>
> sub new {
> my $class = shift;
> my $self = {};
>
> $self{Class} = $class;
> bless $self, $class;
> }
>
> sub getClass{
> my $self = shift;
> $self{Class};
> }
> 1;
> -------------------------------------------------------------
> #ChildA.pm
> package ChildA;
>
> use Parent;
> @ISA = (Parent);
>
> 1;
> -------------------------------------------------------------
> #ChildB.pm
> package ChildB;
>
> use Parent;
> @ISA = (Parent);
>
> 1;
> -------------------------------------------------------------
> #Inheritence.pl
> use ChildA;
> use ChildB;
>
> my $childA = ChildA->new();
>
> print "A: " . $childA->getClass() . "\n";
>
> my $childB = ChildB->new();
>
> print "B: " . $childB->getClass() . "\n";
> print "A: " . $childA->getClass() . "\n";
>
>
>



 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-09-2004
Anthony Roy <(E-Mail Removed)> wrote:

> Four example files are at the bottom of this email

^^^^^^^^^^
^^^^^^^^^^

Usenet is not email!


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
antroy
Guest
Posts: n/a
 
      09-09-2004
Tad McClellan wrote:

> Anthony Roy <(E-Mail Removed)> wrote:
>
>
>>Four example files are at the bottom of this email

>
> ^^^^^^^^^^
> ^^^^^^^^^^
>
> Usenet is not email!


Thanks for the constructive advice. No really. Prick.

--
Ant...
 
Reply With Quote
 
antroy
Guest
Posts: n/a
 
      09-09-2004
Hi Anno,

Anno Siegel wrote:
....
> You're better off using strict (and warnings). Introduce them in
> your code and try again. Also, use Data:umper to see what your
> objects actually contain.
>
> See the problem now?


Yep. The main one being that I was missing the -> when setting values in
the $self hash. Thanks to the pointer to the strict module. I am a long
time OO programmer, but new to Perl, and so tips like these I find valuable.

> Inheritance in Perl does have its issues, but on the level you're
> acting here it works just fine. Your code is in error, in several
> places. Strict will point them out.


And it did

--
Ant...
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-09-2004
antroy <(E-Mail Removed)> wrote:
> Tad McClellan wrote:
>
>> Anthony Roy <(E-Mail Removed)> wrote:
>>
>>
>>>Four example files are at the bottom of this email

>>
>> ^^^^^^^^^^
>> ^^^^^^^^^^
>>
>> Usenet is not email!

>
> Thanks for the constructive advice.



You're welcome.

Can you identify how it is constructive to know the difference?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anthony Roy
Guest
Posts: n/a
 
      09-10-2004
Tad McClellan wrote:
> antroy <(E-Mail Removed)> wrote:
>
>>Tad McClellan wrote:
>>
>>>Usenet is not email!

>>
>>Thanks for the constructive advice.

>
>
>
> You're welcome.
>
> Can you identify how it is constructive to know the difference?


It's called irony. I think it is a rare thing in Texas.

I won't be continuing with this fork of the thread - it's exactly this
sort of irrelevant banter that annoys me about usenet. Someone asks a
technical question in line with the charter, and someone else responds
with "this is usenet not email", or "use google" or other arbitrary or
useless remark.

Why not use the forums for the purpose they are intended, rather than a
place to simply bitch for the sake of it? I'm sure there are plenty of
forums for that such as alt.pedants.misc or similar.

And before you bother to reply, I am fully aware of the irony (that word
again - look it up) of the fact I'm adding to the very rubbish I'm
complaining about but restate that I won't be posting to this fork of
the thread again.

I'm just glad that there *are* people who are keen to help (thanks Anno)
rather than just waste time and bandwidth with irrelevant rubbish.

--
Anthony Roy.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-10-2004
Anthony Roy <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Tad McClellan wrote:
> > antroy <(E-Mail Removed)> wrote:
> >
> >>Tad McClellan wrote:
> >>
> >>>Usenet is not email!
> >>
> >>Thanks for the constructive advice.

> >
> >
> >
> > You're welcome.
> >
> > Can you identify how it is constructive to know the difference?

>
> It's called irony. I think it is a rare thing in Texas.
>
> I won't be continuing with this fork of the thread - it's exactly this
> sort of irrelevant banter that annoys me about usenet.


Then find yourself a forum that complies with your demands and get
the **** out of Usenet. What you get here is raw advice, if you asked
for it or not. If you want it with an ego-protective wrapper, you
got to pay.

Anno
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-10-2004
Anthony Roy <(E-Mail Removed)> wrote:
> Tad McClellan wrote:
>> antroy <(E-Mail Removed)> wrote:
>>>Tad McClellan wrote:
>>>
>>>>Usenet is not email!
>>>
>>>Thanks for the constructive advice.

>>
>>
>>
>> You're welcome.
>>
>> Can you identify how it is constructive to know the difference?

>
> It's called irony.



It is clear that you cannot identify why the difference between
email and usenet might be important, your loss.

But let's discuss it for the benefit of the lurkers.


> I think it is a rare thing in Texas.



If you cannot think of anything to support your position, start
looking around for something _else_ you can attack ad hominem.

Where I live is not related to whether the difference is relevant
or not.

Do you have any arguments supporting why the distinction is
not worth making? Or or you just going to continue with
name calling?


> I won't be continuing with this fork of the thread - it's exactly this
> sort of irrelevant banter that annoys me about usenet.



Your question had already been answered, so I saw no point in
answering it again.

I did, however, notice something else worth correcting, so I corrected it.


> Someone asks a
> technical question in line with the charter, and someone else responds
> with "this is usenet not email", or "use google" or other arbitrary or
> useless remark.



The ultimate goal of a question-asker is to get an answer to the question.

If "use google" leads to the answer, then such advice is NOT useless!


> Why not use the forums for the purpose they are intended,



They are intended to help people learn things, whether it was the
thing they were asking about, or some other thing that they didn't
even know they didn't know.


> rather than a
> place to simply bitch for the sake of it?



It was not just for the sake of it. It was because it makes a difference.

The social dynamic is very different between individual communication (email)
and mass communication (usenet).

You write differently for an audience of thousands than for an individual.

You might expect the recipient to remember what you were talking
about in an email dialog, but that is a poor assumption in a
usenet context. So including the relevant context is important
if writing for usenet and less important if writing for email.

Many people participate in a usenet thread, so trimming the irrelevant
material is important in usenet, less so in email.

If you write to usenet as if it was email, you will look silly,
I though perhaps you would not want to appear that way. My mistake.


> I'm just glad that there *are* people who are keen to help



I have answered tens of thousands of Perl questions on this newsgroup
in the past, and I will answer more in the future, just not any
of yours. Good luck!


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments