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        


In reply to Re^4: Private and Protected class methods (caller) by tye
in thread Private and Protected class methods by radiantmatrix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.