Perl: Variable scoping

Perl allows you scope variables in several ways.

The most common is my which scopes the variable to the surrounding {} (or the entire script/package if there are no {}).

Perl also supports our which is used inside of packages to make a variable global. This allows you to access that variable from a calling script by accessing $Package::variable_name.

Finally there is the local keyword which takes a global variable and makes a locally scoped copy of the variable. Changes to the variable will not be reflected in the global scope.

Name Scope
my Local
our Global
local Temp copy of global
Leave A Reply
All content licensed under the Creative Commons License