isha has asked for the wisdom of the Perl Monks concerning the following question:

when we use a package all the classes & class's methods can be called using class's object?

How can i restrict some of the methods being called?

package A has a methods new, mymethod, mymethod2.

Now, when i create an object($obj) of class A, i can call all its method by
$obj->new(),
$obj->mymethod(),
and $obj->mymethod2().


I want that the mymethod2 should not be allowed to be called.... I want to make it private.
How can i do this?

Replies are listed 'Best First'.
Re: Hiding methods of class
by svenXY (Deacon) on Jan 16, 2008 at 07:42 UTC
Re: Hiding methods of class
by cdarke (Prior) on Jan 16, 2008 at 07:44 UTC
    Store a reference to an anonymous subroutine in a my variable:
    my $sref = sub { print "This has no name\n" }; &$sref(); # Call the subroutine
    Being a my variable, the reference is not visible outside the class.
Re: Hiding methods of class
by Herkum (Parson) on Jan 16, 2008 at 15:55 UTC

    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? ;) ).

      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.

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