Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Howto keep private methods private? ('function' lookup)

by lodin (Hermit)
on Sep 14, 2007 at 13:09 UTC ( [id://639003]=note: print w/replies, xml ) Need Help??


in reply to Howto keep private methods private?

... ways to *ensure* (for my own benefit) that other classes don't use private methods of other classes

Do you mean that you want to ensure that your class (being a parent class) doesn't accidentally call a private method in a subclass? If you're talking about method calls that look like

$self->_private(@args)
then maybe I have something for you.

I don't ever call private methods using method lookup, since I know which package it will be in, namely the current package. I also name private methods with a leading underscore, and thus my code looks like this:

_private($self => @args)
This way, I don't ever have to be afraid to refactor a parent class and add a private method. If you use method lookup, you'll risk accidentally picking up a private method in the subclass, thus needlessly breaking distant code.

This is a paradigm that has worked well for me. It's simple, it doesn't require anything out of the ordinary, and works well enough (but YMMV).

If you talk about what Java calls protected methods (methods that are designed to be used by subclasses too, but no one else), then that's a whole different story.

lodin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://639003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2025-07-08 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.