local - Temporarily save and restore a package variable
You can use local to temporarily save and restore a package variable.
local $VAR = 4;
If no value is specified, undefined value is assinged.
Saving and restoring values
The assigned value using local is restored at the end of scope.
our $NUM = 5; { local $NUM = 3; } # Here, $NUM becomes 5. This is the original value.
If initialization is not performed, undefined value is assinged.
local $NUM;
local is available for the package variable declared by using our. Note that local is not available for a lexical variable declared by using my.
# Compile error my $num; local $num;
However, no exception occurs for the element of array and the value of hash.
my @nums = (1, 2, 3); local $nums[1]; my %score = (math => 90, english => 70); local $score{math};
local for predefined variable
The most common use of local in Perl is for predefined variables such as "$/", "%ENV" and "@ARGV".
local is available for a package variable. Predefined variable actually belong to the CORE package.
So you can use local for predefined variable as well.
local $/; local $ENV{HOME}; local @ARGV;
Let's use my for variable declarations
Since Perl 5.0, Perl introduces the variable declaration by "my", so there is almost no need to declare variable by lcoal. Try to use "my" for almost all variable declarations.
# Variable declaration my $num;
Difference between my and local
local is not a variable declaration unlike what you can imagine from its name. It has a dynamic scope.
The local variable in other languages is the same as the lexical variable of Perl.
local | my |
---|---|
Not a variable declaration | Variable declaration |
Save/restore values | Declare a variable |
Has a dynamic scope | Have a lexical scope |
Read the whole content of a file using local
There are not many opportunities to use local, but there are a some important opportunities. The one is to read the whole content of a file.
I describe the way to read the whole content of a file.
my $file = 'a.txt'; open my $fh, '<', $file or die "Can't open $file:$!"; # Read the whole content of a file my $content = do {local $/; <$fh>};
do block returns the last evaluated value.
predefined variable "$/" is assinged to a undefined value by using local. "$/" is a line separator of files.
line input operator <$fh> returns the whole content of the file.
At the end of scope , "$/" is restored to its original value.
Note that Perl doesn't yet have the builtin function to read the whole content of a file.
local example program
This is an example to temporarily save and restore a predefined variable using local. Predefined variable are a package variable that belong to the CORE package.
{ # Temporary change of predefined variable $/ local $/ = undef; } # Original value is restored when exiting scope
This is an example to understand local.
use strict; use warnings; # local description our $NUM = 1; print "\$NUM = $NUM\n"; # The fully qualified name of $NUM is $main::NUM and belongs to the main package. # (In Perl, no variable is a global variable # Perl has only lexical and a package variable. ) print "\$main::NUM = $main::NUM\n"; print "\n"; { # Temporarily change the value of "$NUM" inside the lexical scope local $NUM = 2; print "In scope\n"; # Output 2 print "\$NUM = $NUM\n"; } print "\n"; # Output 1 print "Out of scope\n"; print "\$NUM = $NUM\n";
Output:
$NUM = 1 $main::NUM = 1 In scope $NUM = 2 Out of scope $NUM = 1