Perl::...Package(3pm)

πŸ‘‘ Perl::Critic::Policy::Modules::RequireExplicitPackage(3pm) β€” the "always make the package explicit" policy, bestie πŸ’…

okay so this is a page from the User Contributed Perl Documentation manual, and it's about one lil Perl::Critic policy that is genuinely so real of it. let's get into it ✨


🌸 NAME 🌸

Perl::Critic::Policy::Modules::RequireExplicitPackage β€” always make the package explicit. periodt. πŸŽ€


πŸ’– AFFILIATION πŸ’–

This Policy is part of the core Perl::Critic distribution. she's built in, she's canon, no cap. 🫢


✨ DESCRIPTION ✨

okay so generally speaking? the first statement of any Perl module or library should be a package statement. like, that's the vibe check. because otherwise β€” and this is the messy part β€” all the code that comes before the package statement is getting executed in the caller's package, and bestie you have no idea who that is. 😭 nobody knows. it's giving mystery guest.

good encapsulation and common decency require your module to keep its innards to itself. slay. πŸ’… that's just manners.

now, there are some valid reasons for not having a package statement at all. but make sure you actually understand them before assuming that you should do it too. πŸ‘€ like, don't just copy someone's vibe without reading the room.

one of those reasons is having a use VERSION line as the first line of your Perl file. it declares which version of the Perl language the following code is written in. and because the effect is lexical, the previous remarks about the caller's package do not apply. so that one's chill. 🌈

also, heads up: the maximum number of violations per document for this policy defaults to 1. one and done, she's not gonna drag you all day. πŸ’€


🌟 CONFIGURATION 🌟

so, as for programs β€” most people understand that the default package is main, so this Policy doesn't apply to files that begin with a perl shebang. 🧚 she minds her own business there.

if you do want to require an explicit package declaration in all files, including programs, then add the following to your .perlcriticrc file:

[Modules::RequireExplicitPackage]
exempt_scripts = 0

some users may find it desirable to exempt the load of specific modules from this policy. for example, Perl does not support Unicode module names because of portability problems. users who are not concerned about this and intend to use UTF-8 module names will need to specify use utf8; before the package declaration. to do this, add the following to your .perlcriticrc file:

[Modules::RequireExplicitPackage]
allow_import_of = utf8

the allow_import_of configuration option takes multiple module names, separated by spaces. πŸ“


πŸ”₯ IMPORTANT CHANGES πŸ”₯

this policy was formerly called ProhibitUnpackagedCode which sounded a bit odd. ngl, she needed a rebrand. πŸ’…

so if you get lots of Cannot load policy module errors, then you probably need to change ProhibitUnpackagedCode to RequireExplicitPackage in your .perlcriticrc file. that's it, that's the fix, you're welcome. πŸ™ˆ


πŸ¦‹ AUTHOR πŸ¦‹

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> β€” icon, legend, the moment. πŸ‘‘


Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.

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.

fr fr, read the license file, it's right there, i'm not doing it for you bestie 🫢

⭐ and that's the whole page!! sign-off time: this was perl v5.42.0, dated 2024-10-23, and yes it's still Perl::...Package(3pm) at the bottom because the footer said so. mwah πŸ’œπŸŒΈ

πŸ“¦ where this page comes from