Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Point me in the right direction with OO inheritance and static variables

by 2teez (Vicar)
on Sep 02, 2014 at 11:15 UTC ( [id://1099250]=note: print w/replies, xml ) Need Help??


in reply to Point me in the right direction with OO inheritance and static variables

hi Amblikai,
If what choroba and tobyink gave are too strong a drink to take all at once, you can check this soft milk

use warnings; use strict; package Animal; sub new { my $class = shift; my $self = { 'name' => shift || 'Ologbo', 'tail' => shift || 'Yes', }; return bless $self, $class; } package Cat; use base 'Animal'; sub nameMe { my $self = shift; return $self->{'name'}; } package main; my $cat = Animal->new; print $cat->{'name'}, $/; # Ologbo my $cat2 = Cat->new('Blackky'); print $cat2->nameMe # Blackky

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
  • Comment on Re: Point me in the right direction with OO inheritance and static variables
  • Download Code

Replies are listed 'Best First'.
Re^2: Point me in the right direction with OO inheritance and static variables
by McA (Priest) on Sep 02, 2014 at 14:38 UTC

    Hi 2teez,

    I'm not really happy with your suggestion for a newcomer. Why?

    1) Choroba presented a simple constructor (as strong as pure water to stay with your metaphor) where you can can overwrite the default values of name and tail independently in a way most people would expect working with constructors in Perl. The positional parameters to new you present is not very common IMHO. So, I think Corion's constructor is really simple enough.

    2) In package main you then do a

    print $cat->{'name'}, $/;

    which means you circumvent encapsulation (yes, I know, for debugging purposes). You show that to a newcommer to whom should be taught the "right" thing.

    3) The point above IMHO comes only from the fact that you define the method nameMe in the child class. Why haven't you defined it in the base class? Then you wouldn't have to break encapsulation in your example and you would have shown inheritance in a nice mini example.

    Be sure, I wouldn't have written this comment when Amblikai wouldn't have been so "excited" about your example. Please consider my comment as a hopefully constructive criticism.

    And yes, Tobyink's answer is a hard drink to newcomers, but worth studying.

    Best regards
    McA

Re^2: Point me in the right direction with OO inheritance and static variables
by Amblikai (Scribe) on Sep 02, 2014 at 11:44 UTC

    Thanks, the way you are doing defaults is a bit simpler and might be sufficient for my program. I'm learning this stuff though so anything is good to see! Cheers!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-18 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found