If you do not declare a variable with my , variables without a full package specification will go into the current package. Here, as you can see the variables used for the first time and what they will be:
my $temp;
local sets the scope of the package variable. When you declare this "dynamic" region, Perl uses the temporary value that you set to the end of the region. As with other package variables, Perl creates them when they are first used. The fact that you can use it first with local in front does not affect this.
Many people who tried to answer your question immediately lured you to strict . This is an auxiliary programming tool that helps you to enter the variable name by mistake, forcing you to declare all the variables that you are going to use. When you use a variable name that you have not declared, it stops compiling your program. You can do this with vars pragma, my , state or our :
use vars qw($temp); our $temp; my $temp; state $temp;
local not part of this, as you saw. What for? Because the way it is. I would like more if everything was different.
strict will not complain if you use the full package specification, for example $Foo::Bar::temp . You can fool all those who never notice.
I basically reserve the use of local for special Perl variables that you don't need to declare. If I want to use $_ in a routine, perhaps to use operators that use $_ by default, I will probably start with local $_ :
sub something { local $_ = shift @_; s/.../.../; tr/.../.../; ...; }
I probably most often use local with an input separator, so I can use different line endings without affecting, perhaps earlier:
my $data = do { local $/; <FILE> };
They work because the first use of those variables that you have not seen is implied.
Otherwise, I probably want to make the variables private for my routine so that nothing outside the routine can see it. In this case, I do not need a package variable that the rest of the program can read or write. This job is for my variables:
sub something { my $temp = ...; }
A programming trick is a limitation of what can happen exactly with what you want. If the rest of your program cannot see or change the variable, my is the way to go.
I explain that this is Learning Perl and writing about the details of package variables in Perl Mastering .