It is common knowledge that you can’t have private functions and methods in Perl5. But it turns out that you can do it, one way is to use namespace::clean. Using this module you can either declare all the names of private functions at the top or use a serie of non-obvious “use namespace::clean
“, “no namespace::clean
” calls.
Wouldn’t it be much nicer just to be able to write:
sub foo :Private {
...
}
You can, with my brand new Sub::Private module. It is actually quite simple:
use Attribute::Handlers;
use namespace::clean qw();
use B::Hooks::EndOfScope qw(on_scope_end);
use Sub::Identify qw(get_code_info);
sub UNIVERSAL::Private :ATTR(CODE,BEGIN) {
my ($package, $symbol, $referent, $attr, $data) = @_;
on_scope_end {
namespace::clean->clean_subroutines( get_code_info( $referent ) );
}
}
Putting the attribute handler in the UNIVERSAL namespace isn’t nice. I have to find a solution for that for the next version.