Perl and Inheritance strangeness.

A

Anthony Roy

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";
 
A

Anno Siegel

Anthony Roy said:
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::Dumper 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
 
A

antroy

Hi Anno,

Anno Siegel wrote:
....
You're better off using strict (and warnings). Introduce them in
your code and try again. Also, use Data::Dumper 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 :)
 
A

Anthony Roy

Tad said:
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.
 
A

Anno Siegel

Anthony Roy said:
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
 
T

Tad McClellan

Anthony Roy said:
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!
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top