Perl::...geVars(3pm)

okay so bestie, welcome to Perl::...geVars(3pm) from the User Contributed Perl Documentation ✨ this is the group chat where we learn to stop leaking package variables, and honestly? iconic behavior only πŸ’… slay, let’s get into it.

🌸 NAME 🌸

Perl::Critic::Policy::Variables::ProhibitPackageVars β€” eliminate globals declared with "our" or "use vars". this policy ate and left no crumbs 🫢

🫢 AFFILIATION 🫢

This Policy is part of the core Perl::Critic distribution, periodt.

πŸ’– DESCRIPTION πŸ’–

Conway suggests avoiding package variables completely, because they expose your internals to other packages β€” it’s giving leaky group chat energy. Never use a package variable when a lexical variable will suffice, fr fr. If your package needs to keep some dynamic state, consider using an object or closures to keep the state private.

This policy assumes that you’re using strict vars so that naked variable declarations are not package variables by default. Thus, the way that it complains is when you declare a variable with our or use vars, or when you make reference to a variable with a fully-qualified package name.

$Some::Package::foo = 1;    # not ok
our $foo            = 1;    # not ok
use vars '$foo';            # not ok
$foo = 1;                   # not allowed by 'strict'
local $foo = 1;             # bad taste, but technically ok.
use vars '$FOO';            # ok, because it's ALL CAPS
my $foo = 1;                # ok

vibe check: use vars '$FOO'; is ok because it’s ALL CAPS, and my $foo = 1; is giving green flag energy 🌈. not me side-eyeing local $foo = 1; though β€” bad taste, but technically ok πŸ˜­πŸ’€

In practice though, it’s lowkey not really practical to prohibit all package variables. Common variables like $VERSION and @EXPORT need to be global, as do any variables that you want to Export. To work around this, the Policy overlooks any variables that are in ALL_CAPS. ngl, this highkey forces you to put all your exported variables in ALL_CAPS too, which seems to be the usual practice anyway. obsessed with this workaround, honestly 🧚

πŸŽ€ CONFIGURATION πŸŽ€

There is room for exceptions. Some modules, like the core File::Find module, use package variables as their only interface, and others like Data::Dumper use package variables as their most common interface. These module can be specified from your .perlcriticrc file, and the policy will ignore them.

[Variables::ProhibitPackageVars]
packages = Data::Dumper File::Find FindBin Log::Log4perl

This is the default setting, no cap. Using packages = will override these defaults.

You can also add packages to the defaults like so:

[Variables::ProhibitPackageVars]
add_packages = My::Package

You can add package main to the list of packages, but that will only OK variables explicitly in the main package. not me adding main and expecting every variable ever to pass β€” it only OKs the ones explicitly in the main package, bestie πŸ™ˆ iykyk.

πŸ¦‹ SEE ALSO πŸ¦‹

  • Perl::Critic::Policy::Variables::ProhibitPunctuationVars
  • Perl::Critic::Policy::Variables::ProhibitLocalVars

πŸ‘‘ AUTHOR πŸ‘‘

Shoutout to Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> for this one πŸ‘‘

Copyright (c) 2005-2023 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.

k bye bestie, this Perl::...geVars(3pm) page came from perl v5.42.0, dated 2024-10-23 β€” go keep your state private πŸ’œβœ¨

πŸ“¦ where this page comes from