Perl::...ackage(3pm)

πŸ’– NAME πŸ’–

okay bestie, welcome to the Perl::...ackage(3pm) page β€” yes, that compressed manual name is lowkey iconic. The policy we're talking about is Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage, and its whole personality is: Package declaration must match filename. πŸ’…

πŸŽ€ AFFILIATION πŸŽ€

This Policy is part of the core Perl::Critic distribution. No cap, it's core-core. ✨

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so the package declaration should always match the name of the file that contains it. For example, package Foo::Bar; should be in a file called Bar.pm. Why do we care? Because it makes it easier for developers to figure out which file a symbol comes from when they see it in your code. For instance, when you see Foo::Bar->new(), you should be able to find the class definition for a Foo::Bar in a file called Bar.pm.

Therefore, this Policy requires the last component of the first package name declared in the file to match the physical filename. Or if #line directives are used, then it must match the logical filename defined by the prevailing #line directive at the point of the package declaration. Here are some examples:

  # Any of the following in file "Foo/Bar/Baz.pm":
  package Foo::Bar::Baz;     # ok
  package Baz;               # ok
  package Nuts;              # not ok (doesn't match physical filename)

  # using #line directives in file "Foo/Bar/Baz.pm":
  #line 1 Nuts.pm
  package Nuts;             # ok
  package Baz;              # not ok (contradicts #line directive)

If the file is not deemed to be a module, then this Policy does not apply. Also, if the first package namespace found in the file is main then this Policy does not apply. 🫢

🌸 CONFIGURATION 🌸

Nope, no extra knobs to twiddle here, bestie: this Policy is not configurable except for the standard options. πŸ’…

🧚 AUTHOR 🧚

Chris Dolan <cdolan@cpan.org> β€” iconic. πŸ’–

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.

okay love u, bye! πŸ’œ this was perl v5.42.0, dated 2024-10-23, from the Perl::...ackage(3pm) manual β€” slay and stay validated ✨

πŸ“¦ where this page comes from