strict “vars”
Under this stricture, a compile-time error is triggered if you try to access a variable that hasn’t met at least one of the following criteria:
Predefined by Perl itself, such as
@ARGV,%ENV, and global punctuation variables like$.or$_.Declared with
our(for a global) ormyorstate(for a lexical).Imported from another package. (The
varspragma fakes up an import, but useourinstead.)Fully qualified using its package name and the double-colon package separator.
The local operator by itself
isn’t good enough to keep use strict
"vars" happy because, despite its name, that operator doesn’t
change whether the named variable is globally visible. Instead, it gives
the variable (or individual element of an array or hash) a new,
temporary value for the duration of the block at runtime. You still need
to use our to declare a global
variable, or my or state to declare a lexical variable. You can,
however, localize an our:
local our $law = "martial";
Globals predefined by Perl are exempt from these requirements.
This applies to program-wide globals (those forced into package main like @ARGV or $_) and to per-package variables like $a and $b,
which are normally used by the sort
function. Per-package variables used by modules like Exporter must still be declared using our:
our @EXPORT_OK = qw(name rank serno);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access