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

    I'd think you'd want to know that whoever is calling the _protected_method() ->can( "_protected_method" )

    That's correct, and actually -- it works. The level for caller took some playing with, but caller(1) turns out to be right: caller(0) would be the _protected_sub itself, while caller(1) is whomever called the _protected_sub.

    I'd name this method _PROTECTED()

    Good point, noted.

    In particular, consider a case where I've "exported" a sub into my method namespace.

    Hm, interesting. I hadn't considered what would happen when exporting methods like that, since I've never had need to do things like that. I will have to bring it up with my design team and see what to do about it. That particular case seems to work (though I don't entirely understand why), but I can see where more complicated cases in the same class might cause problems.

    Thank you, and ++;

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet