|Think about Loose Coupling|
Private method variationsby adrianh (Chancellor)
|on Mar 01, 2004 at 00:42 UTC||Need Help??|
While tidying up some code recently I was pondering the problems with the use of the _method_name() convention to indicate private methods in Perl.
Of course their are many alternative conventions that prevent the accidental overriding of methods in subclasses, for example:
They also all have disadvantages of one sort or another, for example:
With the package name and subroutine calling methods we can prevent the method from being inherited by subclasses by putting it in a seperate package (see (tye)Re: Private Class Methods for an example). So we can do things like:
Which is nice, but we still have to repeat the Foo::private package every time we call the method - which for VeryLongPackageNames could be tedious.
A convenient shortcut to the private method space would be nice. Maybe something like:
This fits in quite nicely with SUPER:: and NEXT::.
This is actually pretty trivial to implement - just stick an AUTOLOAD in the MY package:
and Bob's the parental sibling of your choice.
Unfortunately this adds an extra method call worth of overhead to every private method. Probably a bad move - Perl's method invocation is slow enough as it is.
It would also be nice to be able to do:
Having to repeat MyLongPackageName for every private method definition is a pain.
Looks like a job for (and I don't say this very often because they're evil :-) a source filter.
Which will allow us to write code like this:
Without the two private greet_world() methods of Foo and Bar interfering, and without any run-time overhead.
Worth throwing at CPAN?