Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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
  • Private but not really private Methods

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-24 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found