How to keep a method private in an OO mix-in module.

markjugg on 2005-08-08T20:38:18

In CGI::Application-based applications, it's often handy to have a module that mixes-in methods into a primary module, which simply means that the methods are added using standard "has a" export/import techniques. That's what a number of the C::A plugins do.

There is a sticking point though. The mix-in may want to call an internal method, as:

$self->_internal_method(@args)

However, the way the OO works, this method is expected in the package that has been mixed into, although you don't want to export it there.

Cees Hek pointed out a simple solution to this, which I managed to miss for several years:

_internal_method($self,@args);

Passing in $self like that keeps the method internal to the mix-in as desired.