in reply to Private and Protected class methods
There are no end of solutions to make method subs private, the most common one I've seen being some variation on:The result is that only the package where _private_sub is defined can invoke it as a method. I've used this in the past where I found it sensible, and it has always worked well enough.sub _private_sub { die "_private_sub is private!" unless caller eq __PACKAGE__; # private stuff # }
While this is a common way to implement private method, it is completely wrong.
If the perl method dispatcher encounters a private method, it should not die, but instead it should skip it an continue the dispatch. Take this pseudo-perl example:
Now if under your scheme, this code:package Foo; sub bar : public { ... } package Bar; use base 'Foo'; sub bar : private { ... } package Baz; use base 'Bar';
would die, but that is not correct, it should succeed and call Foo::bar.Baz->new->bar;
Method dispatching which includes Private/Protected/Public can have many subtle edge cases, some of which are not easily (if at all) solveable with the basic mechanisms provided in perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Private and Protected class methods
by radiantmatrix (Parson) on Sep 07, 2006 at 15:01 UTC |