My first impression of the above solution is that is appears broken anyway {it seems like it should be seeing if (caller(2)'s package)->can(caller(1)'s method name)}. So I probably don't understand the point of it.
Actually, no (I have tested this). The subroutine name from caller is in the form Package::sub_name, where Package is always the name of the definining package. So, I check to see "who invoked the sub that called me?" and see if that package can sub_name. It works. It took me a while to get right, but it works.
And I doubt I'd use inheritance to get this helper subroutine available to every class that wants to define protected subs.
That's not exactly what I'm doing. This particular client has asked me to write an organization-wide base class (as in "all our classes will inherit from this"). This feature is being included there, so it must propogate via inheritance. That's not a design decision, it's a requirement.
I'm not sure that the way @ISA is searched using can() is the best approach here.
See, it's the only way I know of. I'd love to hear if there was another, better way. Care to elaborate?
No, I suspect that my OO "best practices" would break this code.
Can you be more specific? What do you do that would break this? How would it break?
I also suspect that stepping back and looking at the specific problem may lead to a solution other than "protected methods".
Like I said in my intro -- my personal feeling is to simply establish a coding standard that has a convention for naming protected and private methods, and let the code reviews, etc. catch things. That said, I also understand my client's point of view -- sometimes it's nice to kvetch about things like this (like the Perl::BestPractices module).
The central point being that the ability to define private and protected methods is a requirement of my client, regardless of how I feel about them, so I need to find a workable solution that won't cause too many issues. You seem to suggest that the solution I'm proposing would cause some issues, but I would appreciate expansion of what those might be and what I have to consider to devise a work-around.
In reply to Re^3: Private and Protected class methods (accidental)
by radiantmatrix
in thread Private and Protected class methods
by radiantmatrix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |