Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to do this?

by kcott (Abbot)
on Jun 22, 2013 at 02:05 UTC ( #1040241=note: print w/ replies, xml ) Need Help??


in reply to How to do this?

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


Comment on Re: How to do this?
Select or Download Code
Re^2: How to do this?
by ryo0ka (Novice) on Jun 22, 2013 at 18:25 UTC
    Thank you Ken so much!! I really appreciate your examples :DD

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040241]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-22 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls