Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [LanX]: memories....I missed my connection while chatting
    [Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
    [Discipulus]: rend out the xls IF i have access to the DB
    [choroba]: LanX I miss working in a bank sometimes...
    [Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
    [Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
    [Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
    [LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (12)
    As of 2017-03-29 12:10 GMT
    Find Nodes?
      Voting Booth?
      Should Pluto Get Its Planethood Back?

      Results (350 votes). Check out past polls.