Perl::...ateSubs(3pm)

πŸ’… NAME

okay so bestie, this is Perl::Critic::Policy::Subroutines::ProtectPrivateSubs β€” the manual page lives under User Contributed Perl Documentation in section 3pm, and what it does is Prevent access to private subs in other packages. periodt. it's literally a bodyguard for your underscore-prefixed subs 🫢


✨ AFFILIATION ✨

This Policy is part of the core Perl::Critic distribution. so it ships with the whole thing, no extra install drama πŸ’…


🌸 DESCRIPTION 🌸

okay so here's the vibe: by convention, Perl authors (like authors in many other languages, fr fr) indicate private methods and variables by inserting a leading underscore before the identifier. So when you see _something, that's the author going "hands off, this is mine" πŸ™ˆ

This policy catches attempts to access private variables from outside the package itself. Like, don't be nosy. it's giving uninvited guest energy 😭

But! The subroutines in the POSIX package which begin with an underscore (e.g. POSIX::_POSIX_ARG_MAX) are not flagged as errors by this policy. Those get a free pass, iconic behavior honestly πŸ‘‘


πŸŽ€ CONFIGURATION πŸŽ€

You can define what a private subroutine name looks like by specifying a regular expression for the private_name_regex option in your .perlcriticrc:

[Subroutines::ProtectPrivateSubs]
private_name_regex = _(?!_)\w+

The above example is a way of saying that subroutines that start with a double underscore are not considered to be private.

lowkey important lore: (Perl::Critic, in its implementation, uses leading double underscores to indicate a distribution-private subroutine β€” one that is allowed to be invoked by other Perl::Critic modules, but not by anything outside of Perl::Critic.) so that's the whole reason this regex exists, iykyk 🧚

You can also configure additional subroutines to accept by specifying them in a space-delimited list to the allow option:

[Subroutines::ProtectPrivateSubs]
allow = FOO::_bar FOO::_baz

These are added to the default list of exemptions from this policy. πŸ’–

And here's the cute part: allowing a subroutine also allows the corresponding method call. So FOO::_bar in the above example allows both FOO::_bar() and FOO->_bar(). two for the price of one, slay ✨


πŸ¦‹ HISTORY πŸ¦‹

This policy is inspired by a similar test in B::Lint. so it's got lore, it's got lineage πŸ‘‘


πŸ’€ BUGS πŸ’€

Doesn't forbid $pkg->_foo() because it can't tell the difference between that and $self->_foo().

yeah… it literally cannot tell bestie. the vibes are identical. a tragedy 😭

🌈 SEE ALSO 🌈

  • Perl::Critic::Policy::Variables::ProtectPrivateVars β€” the sibling policy, same energy, different target πŸ“

⭐ AUTHOR ⭐

Chris Dolan <cdolan@cpan.org>


Copyright (c) 2006-2011 Chris Dolan.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.


and that's the whole page bestie, no cap 🫢 β€” brought to you by perl v5.42.0, dated 2024-10-23, filed under Perl::...ateSubs(3pm). go forth and respect the underscores πŸ’…βœ¨

πŸ“¦ where this page comes from