http://qs321.pair.com?node_id=368995


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