That approach to method privacy is fundementally broken, consider this:
This is incorrect behavior, private methods should not block a perfectly valid (inherited) public method from being called. The only way to get proper behavior is to not add the private method to the class's symbol table, which means doing it like so:package Foo; sub new { bless {} => shift } sub baz { "Hello from Foo::baz" } package Bar; use base 'Foo'; sub baz { # baz is private ... my $caller = caller; croak "Can't call private method from $caller" if $caller ne __PACKAGE__; "Hello from Bar::baz (private)"; } sub call_baz { (shift)->baz } package main; Bar->new->baz; # BOOM!
package Foo; sub new { bless {} => shift } sub baz { "Hello from Foo::baz" } package Bar; use base 'Foo'; my $baz = sub { # baz is private ... my $caller = caller; croak "Can't call private method from $caller" if $caller ne __PACKAGE__; "Hello from Bar::baz (private)"; }; sub call_baz { (shift)->$baz() } package main; Bar->new->baz; # no more BOOM!
In reply to Re^2: Howto keep private methods private?
by stvn
in thread Howto keep private methods private?
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |