Dec
14
2008
Faking “Private” methods with Perl
Author: Jeremy SmythAlthough Perl doesn’t really do the whole OO concept of private/public access modifiers, it’s somewhat possible to approximate them using subroutine references.
Now we have a lexically scoped variable, visible only within its package, containing a reference to an anonymous sub.
Within our class/package, we call it by doing something like:
&$print_rev("Reversed!");
Voila! Instant private method!
note: this is partway to creating closures, which are functions declared at runtime with, with variables passed as parameter. I might mention them some other time.
Tags: Object-Oriented, Perl, Private