Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

G'day ryo0ka,

Your main problem here is that you're ignoring the first argument passed to the class method new() (i.e. Son) and the first argument passed to the object method call() (i.e. $obj).

A secondary problem is that the constructor, new(), should probably be in the Parent class.

Here's a rewrite of your code to demonstrate this:

$ perl -Mstrict -Mwarnings -le ' package Parent; sub new { bless {} => $_[0] } sub call { $_[0]->char } sub char { "parent" } package Son; use base "Parent"; sub char { "son" } package main; my $parent_obj = Parent->new; print "Parent object char = ", $parent_obj->call; my $son_obj = Son->new; print "Son object char = ", $son_obj->call; ' Parent object char = parent Son object char = son

Additional example:

While it's absolutely fine that you've posted minimal code to show your problem, it occurred to me that the following might be closer to what you were trying to achieve.

$ perl -Mstrict -Mwarnings -le ' package Parent; sub new { bless {} => $_[0] } sub call { $_[0]->char } sub char { ref $_[0] } package Son; use base "Parent"; package main; my $parent_obj = Parent->new; print "\$parent_obj class = ", $parent_obj->call; my $son_obj = Son->new; print "\$son_obj class = ", $son_obj->call; ' $parent_obj class = Parent $son_obj class = Son

Note that there is no longer any hard-coded "parent" or "son" strings and that the Son class now has no methods at all; this code now relies entirely on inheritance to return the same information you had previously with two hard-coded char() methods.

See how easy it is to extend the class hierarchy without needing to write a custom char() method for every new subclass:

$ perl -Mstrict -Mwarnings -le ' package Parent; sub new { bless {} => $_[0] } sub call { $_[0]->char } sub char { ref $_[0] } package Son; use base "Parent"; package Daughter; use base "Parent"; package Grandson; use base "Son"; package main; my $parent_obj = Parent->new; print "\$parent_obj class = ", $parent_obj->call; my $son_obj = Son->new; print "\$son_obj class = ", $son_obj->call; my $daughter_obj = Daughter->new; print "\$daughter_obj class = ", $daughter_obj->call; my $grandson_obj = Grandson->new; print "\$grandson_obj class = ", $grandson_obj->call; ' $parent_obj class = Parent $son_obj class = Son $daughter_obj class = Daughter $grandson_obj class = Grandson

-- Ken


In reply to Re: How to do this? by kcott
in thread How to do this? by ryo0ka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (12)
    As of 2014-09-22 09:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (185 votes), past polls