Perl does not have a classical system of access modifiers like public/private/protected. All access control to functions and data is handled through conventions and the scope of variables.
my keyword is used (lexical visibility within the file or block) and symbols are exported (through Exporter or custom mechanisms).@EXPORT, @EXPORT_OK) remain "internal" to the module by default, although they can always be called explicitly with the package prefix if desired._internal_sub), which serves as a privacy convention.Example:
package MyLib; use Exporter 'import'; our @EXPORT_OK = qw(public_func); my $secret = 123; # Not accessible from outside sub public_func { _internal_func(); } sub _internal_func { print "This is internal! "; } 1;
What measures protect a private function of a Perl module from being called from outside the code? Can it be called directly?
Answer: In fact, any subroutine of the package can be called explicitly with the full name: MyLib::_internal_func(). Perl does not technically restrict this call - protection is only at the level of convention.
Story
A developer exported too many functions from the module, forgetting to limit using
@EXPORT_OK. As a result, users accidentally gained access to internal functions, leading to name conflicts and incorrect API usage.
Story
In a large project, two teams used the same internal method (starting with _), thinking it was private. Later, they changed the function interface, which broke calls from another part of the system.
Story
In one project, they modified an external developer's module by directly calling its unexported methods. After updating the distribution, the module stopped working because the internal function names changed, causing the application to crash.