The following code illustrates the difference between
my
and
local.
Note that if a variable is declared local inside a function,
then any function calls inside that function
will have access to this local variable
provided these other functions does not further localize the variable.
---------------------------------------------
EXAMPLE CODE
---------------------------------------------
sub set_x_to_0{
$x = 0;
}
sub set_my_x_to_1{
my $x = 1;
&set_x_to_0;
print "Result of set_my_x_to_1 is $x";
}
sub set_local_x_to_2{
local $x = 2;
&set_x_to_0;
print "Result of set_local_x_to_2 is $x";
}
&set_my_x_to_1;
&set_local_x_to_2;
---------------------------------------------
RESULT OF EXECUTION OF ABOVE CODE
---------------------------------------------
Result of set_my_x_to_1 is 1
Result of set_local_x_to_2 is 0