Perl and Inheritance strangeness.

Discussion in 'Perl Misc' started by Anthony Roy, Sep 9, 2004.

  1. Anthony Roy

    Anthony Roy Guest

    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";
     
    Anthony Roy, Sep 9, 2004
    #1
    1. Advertising

  2. Anthony Roy

    Anno Siegel Guest

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

    > -------------------------------------------------------------
    > #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";
    >
    >
    >
     
    Anno Siegel, Sep 9, 2004
    #2
    1. Advertising

  3. Anthony Roy <> wrote:

    > Four example files are at the bottom of this email

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

    Usenet is not email!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 9, 2004
    #3
  4. Anthony Roy

    antroy Guest

    Tad McClellan wrote:

    > Anthony Roy <> wrote:
    >
    >
    >>Four example files are at the bottom of this email

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


    Thanks for the constructive advice. No really. Prick.

    --
    Ant...
     
    antroy, Sep 9, 2004
    #4
  5. Anthony Roy

    antroy Guest

    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 :)

    --
    Ant...
     
    antroy, Sep 9, 2004
    #5
  6. antroy <> wrote:
    > Tad McClellan wrote:
    >
    >> Anthony Roy <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 9, 2004
    #6
  7. Anthony Roy

    Anthony Roy Guest

    Tad McClellan wrote:
    > antroy <> 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.
     
    Anthony Roy, Sep 10, 2004
    #7
  8. Anthony Roy

    Anno Siegel Guest

    Anthony Roy <> wrote in comp.lang.perl.misc:
    > Tad McClellan wrote:
    > > antroy <> 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
     
    Anno Siegel, Sep 10, 2004
    #8
  9. Anthony Roy <> wrote:
    > Tad McClellan wrote:
    >> antroy <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 10, 2004
    #9
    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. Munnki
    Replies:
    2
    Views:
    531
    Munnki
    Dec 18, 2003
  2. Joe
    Replies:
    3
    Views:
    476
  3. Rouslan Korneychuk
    Replies:
    8
    Views:
    613
    Rouslan Korneychuk
    Feb 10, 2011
  4. Harold Hausman

    Strangeness with ruby-web and sqlite3

    Harold Hausman, Jul 17, 2006, in forum: Ruby
    Replies:
    2
    Views:
    171
    Harold Hausman
    Jul 17, 2006
  5. Ian
    Replies:
    4
    Views:
    132
    Jeff 'japhy' Pinyan
    May 13, 2004
Loading...

Share This Page