in reply to Re^3: Private and Protected class methods (accidental)
in thread Private and Protected class methods
But caller() is being used from PROTECTED(), and I'd think you'd want to know that whoever is calling the _protected_method() ->can( "_protected_method" ) so I'd think you'd want to grab the method name from caller(0) not caller(1) (after experimenting to figure out how many levels up caller($n) looks since the documentation isn't clear enough). But I freely admit that I don't grok the situtation and so could easily be mistaken (in particular, it looks like the fourth value from caller isn't as I expected).
See, it's the only way I know of. I'd love to hear if there was another, better way.
My first impression is that I'd check (calling package)->isa(called package). But I haven't thought any of the options through enough to declare any of them "better".
Given your requirements, I'd name this method _PROTECTED() since it isn't meant to be used by your object users.
See Re: How do YOU do OO in Perl? (constant offsets) and Re: OO - best way to have protected methods (packages) for some quick examples of my use of packages that might break your assumptions. In particular, consider a case where I've "exported" a sub into my method namespace. So caller will return the name of the package that the code was compiled into which isn't helpful here. Will caller return the name of the sub as it was defined or as it was called (since I might be calling My::Class::_protected_sub which got into My::Class by exporting Some::Other::Package::_other_name, for example)? Try this example:
package Caller::CompiledIn; sub origCaller { shift(@_)->_protected_method(); } package Protected::CompiledIn; sub origProtected { my $self= shift(@_)->_PROTECTED(); ... } package main; @Protected::Class::ISA= 'Your::Uber::BaseClass'; *Protected::Class::_protected_method= \&Protected::CompiledIn::origPro +tected; @Caller::Imported::ISA= 'Protected::Class'; *Caller::Imported::importedCaller= \&Caller::CompiledIn::origCaller; my $obj= bless [], 'Caller::Imported'; $obj->importedCaller();
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Private and Protected class methods (caller)
by radiantmatrix (Parson) on Sep 06, 2006 at 20:39 UTC |