Yes, you can. But it's not entirely straightforward, and does it really matter? Perl has a convention that thou shalt not call methods outside your package that start with an underscore. But, if you know what you're doing, you still can. After all, the module author may be wrong and that function might really be needed. I know I've been on both ends of that before: both needing a function that was deemed private (started with a leading underscore), and marking a function private with the underscore that was later needed elsewhere. So it's nice that perl doesn't shackle the developer.

If you really need to, you can do something like this in your SOD::MyOOInterface package (or wherever you want the private method):

my $_some_method = sub { # method here. }; sub externalised_method { #... $_some_method->(@some_args); # or $self->$_some_method(@some_args); #... }
Because it's an anonymous function, no one who can't see the variable that holds its reference will be able to call it. But because it's a lexical to the current package, only methods/functions in the current package can see the variable, thus making it effectively hidden to anyone else: private.

Now, if you want protected equivalent, I don't know how to do that ... but, again, don't: that's not the perlish way.


In reply to Re: perl OO private methods by Tanktalus
in thread perl OO private methods by ennuikiller

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.