Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: How can I add private data/methods in parent class?

by pijush (Scribe)
on Jun 23, 2004 at 10:31 UTC ( #368995=note: print w/replies, xml ) Need Help??


in reply to Re: How can I add private data/methods in parent class?
in thread How can I add private data/methods in parent class?

Thank you all the monks who have replied with so much explation.
Today I was reading Programmaing Perl, 3rd Edition by Larry Wall, Tom Christiansen and Jon Orwant and found out a way to modify the code which I posted in my previous mail to implement private method. The modified code is
{ package Animal; use Carp; use overload(); sub new { my $class = shift; my $name = shift; bless \$name, $class; } sub name { my $self = shift; ref $self ? $$self : "an unnamed $self"; } sub speak { my $class = shift; print "a $$class goes ", $class->sound, "!\n"; } sub eat { my $self = shift; my $food = shift; print $self->name, " eats $food.\n"; } my $private_method = sub { my $self = shift; print "From private method\n"; }; sub Private_Method { my $self = shift; print "kk::$self->{private_var}\n"; if (0 == index overload::StrVal($self), __PACKAGE__,0) { $self->$private_method(); } } } { package Horse; use vars '@ISA'; @ISA = qw (Animal); sub sound {"neigh"}; } { package Sheep; use vars '@ISA'; @ISA = qw (Animal); sub sound {"blahhhhhhhh"}; } my $horse1 = Horse->new("H1"); $horse1->speak; print "Horse sounds ", Horse->sound, "\n"; $horse1->eat("hay"); Sheep->eat("grass"); Sheep->speak(); #$horse1->Private_Method; #I can not access this Animal->Private_Method;
Is this method OK? Is there any loophole? Any help will be really appreciated.
Thanks in advance.
-Pijush

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2022-05-18 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?