Private Methods (as I understand them)
A private method is unaccessible to anthing other then the owning package. The programmer's intent is to keep other objects/scripts from using these methods.

I imagine these methods need to know things that subclasses shouldn't/wouldn't know, or if called at the wrong time/place could mess up the object in ways that would make your head explode. Violently.

 

How can I make private methods?
Perl has no built in contruct to make methods private and I initally thought this was a good idea. If I _really_ want to use a method you consider private I should be able to. And if it makes my program crash or sing Three Dog Night songs then, its my problem.

The ways I've found to 'fake' private methods involve using code references. I didn't come up with these ideas myself, but saw them mentioned in different posts -- well, the first one anyway. And the second is an extention of the first. I'm calling these:

Very Private Methods:
you can simply put a code reference in a lexically scoped (I hope I'm using that term right) variable ... like my $method. Here's an example:

package SomeObject; my $very_private = sub { ... } # ------------------------------------------ # to call this method: $self->$very_private();

This method is only callable from within the package the subroutine is in. $object->$very_private(); won't work from wherever you actually created the object, so this fits the description of a private method. However, I'm not really thrilled with making it ~impossible~ to reach in and use it so I tried:

Private but not really private Methods:

package SomeObject; our $Semi_Private_Method = sub { ... }

Now we've got a method that's easy to call within the package that defines it ($self->$Semi_Private_Method();) but can also be called from outside if you REALLY want to ....

$self->$package_of_method::Semi_Private_Method();

 

Which one to use?
Well, all this fiddling around left me with this conclusion: NEITHER. I understand wanting to have private methods, and making it impossible for an inheriting class to accidently override them ... but I enjoy the idea that I can get at private stuff if I really want/need to.

So ... after all of this I decided on an old standard. Private sub names prefixed with an _ and, to prevent an inheriting class from accidently overriding a private method ... name them _packagename_subname.

I know a lot of programmers insist that you need private methods, but thats like saying 'I expect programmers using my class to be idiots'. And, well, if they're idiot, then too bad. :P

theAcolyte
On the Journy to Object Orientedness


In reply to Private Methods Meditation by theAcolyte

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.