in reply to Hiding methods of class

Perl commonly marks private methods with a leading underscore. Example:

sub new {} # construct a new object sub mymethod {} # public method for an object sub _mymethod2 {} # private method for an object

There is no technical way to prohibit someone from using a private method. You can write lots of code to try to restrict access but they can undo whatever you have written!

So I recommend you just use a leading underscore and don't describe _mymethod2() as a public interface in your POD( you are writing documentation, right? ;) ).

Replies are listed 'Best First'.
Re^2: Hiding methods of class
by Joost (Canon) on Jan 16, 2008 at 22:16 UTC
    There is no technical way to prohibit someone from using a private method. You can write lots of code to try to restrict access but they can undo whatever you have written!
    As cdarke posted, and I also mentioned in the Howto keep private methods private? thread, you can restrict access very efficiently, at compile time, to any lexical variable (including function refs, which can also be used as methods) by putting the variable in a limited scope. This should prevent anyone who isn't using XS internals hacking (like PadWalker) from getting at the method.

    This strategy also has the advantage that it doesn't take up names in the inheritance tree, so you won't accidentally block out (or be blocked out) by sub or super classes. On the other hand that means you can't use it to create "protected" methods. But protected methods are the spawn of Satan, so you shouldn't care about that.

      If the code is on someone else's box, they can change anything and everything you have written. If they can do that then there is no way you can have a guaranteed private methods. That was my point.

Re^2: Hiding methods of class
by parv (Parson) on Jan 16, 2008 at 22:53 UTC
    Perl commonly marks private methods with a leading underscore

    People -- neither Perl nor perl -- use the convention to mark a sub private with a leading underscore. The opening sentence is quite misleading, even though you later qualify that with "[t]here is no technical way to prohibit someone from using a private method".