I'm writing an object oriented module, and I wanted to be
able to emulate c++'s protected method. That is, the method
may be called by its own object, or an object that inherits
from it. But it should not be usable otherwise.
I came up with this. It uses the __PACKAGE__ symbol to
find out what package the method is located in. Maybe
somebody has a better way, or can tell me if something is
wrong with what I'm doing?
This seems like something a lot of people would want to do,
and a rather simple solution, so please don't get upset with
me if somebody else has already come up with it before me.
package foo;
sub whatever {
my $self = shift;
unless ( UNIVERSAL::isa( $self, __PACKAGE__ ) ) {
die( "Can't do that!" );
}
...
}