Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"

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

by dakkar (Hermit)
on Jun 22, 2004 at 14:00 UTC ( #368724=note: print w/ replies, xml ) Need Help??

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

OK, everybody is going to give you different answers, from "you can't do that" to "you shouldn't do that"... I'll try to explain the "why"

Perl has, per se, non concept of "private" methods: a package is a name space: it contains names for things (subroutines, but also variables, etc). OOP in Perl is based on the fact that, by blessing a reference, you attach to it the name of a package. Then, when you use it with the "arrow notation", Perl looks at the name of the package you have attached to the reference, and starts from it to look for a subroutine with the name you specified (I assume you know how the inheritance works). At no point Perl consults some attribute of the reference, the packages, or the subroutines, to discover if it was OK for you to call that subroutine.

This is a design choice. Maybe not the best one (Perl6 will have proper privacy of members), but the one that has been made. The idea was that it was both easier and more flexible to do it this way, and that the programmers would just avoid to step on each other's toes.

There are several modules out there that muck with the inheritance, add methods to other classes, and generally behave pretty poorly if you judge them "by the book". But the main goal, in Perl, is to get the job done. If you need strong encapsulation and privacy, there are modules that can give them to you (you'll get plenty of pointers from other people, I'm feeling a bit lazy at the moment); and the reason that you can get them is that Perl does not provide them itself, but instead leaves everything wide open for anybody to tinker with.

Oh, as a sidenote, Perl6 will both have privacy and encapsulation, and permit to tinker with classes at will. It will just be a little better presented ;-)

        dakkar - Mobilis in mobile

Most of my code is tested...

Perl is strongly typed, it just has very few types (Dan)

Comment on Re: How can I add private data/methods in parent class?
Re^2: How can I add private data/methods in parent class?
by pijush (Scribe) on Jun 23, 2004 at 10:31 UTC
    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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-04-18 22:28 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (472 votes), past polls