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::ProhibitPunctuationVarsPerl::Critic::Policy::Variables::ProhibitLocalVars
π AUTHOR π
Shoutout to Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> for this one π
π COPYRIGHT π
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
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms