Local Scope and Variables

A local variable, as you learned on Day 11, “Creating and Using Subroutines,” is one that is only available to a certain part of a script. After that part of the script is finished executing, the variable ceases to exist. Local variables also have a limited availability to other parts of a script. They might be private to just the scope in which they were defined, or they might only be available to those parts of the script that are running at the same time as the part of the script that defined the variable.

Local Variables and Local Scope

On Day 11, we looked at local variables, defined with my, inside subroutines. A subroutine defines a local scope, and the local variable is available to all the code defined inside that subroutine.

A subroutine isn't the only thing that can create a local scope, however. Any block surrounded by brackets defines a local scope, and any local variables defined within that block will cease to exist at the end of the block. This enables you to define local variables inside loops or conditionals, or even inside bare blocks, if there's some portion of code that will benefit from a new local scope.

All the following snippets of code define a local scope. Each declaration of the variable $s is local to that enclosing block, and each $x is different from all the other versions of $x. Note, in particular, the use of a local variable in the foreach loop; here the variable is local to the entire loop, not to each iteration. $x will be incremented five times, as you'd expect:

if ($foo) {      # conditional
    my $x = 0;
    print "X in conditional: $x
";
    $x++;
}

foreach (1..5) {  # loop
    my $x += $_;
    print "X in loop: $x
";
}

{                # bare block
    my $x = 0;
    print "X in bare block: $x
";
    $x++;
}

One other rule of variables and scope to always keep in mind: Local variables with the same name as global variables hide the values of the global variable of the same name. The value of the local variable is used throughout the current local scope, and then the original global and its original value will be restored at the end of that scope.

Although this is a convenient feature, it can also be terribly confusing. Generally it's a good idea to avoid naming your locals the same name as your globals unless you've got good reasons for doing so.

Alternately, you can always refer to a global variable using its complete package name, even from inside a local scope. This technique assumes you did not use use strict or declare your globals with my:

$foo = 0;  # global
{
   my $foo = 1;        # local
   print "$foo
";     # prints 1
   print "$main::foo
";  # prints 0
}
print "$foo
";        # prints 0
						

Local Variables with my and local

In addition to the local variables defined with my, there are also local variables defined with local. The local modifier is used the same way as the my modifier, with one or more variable names:

local ($x, $y);

What's the difference? The most obvious difference between my local variables and local local variables is that the scope for local local variables is determined by the execution of the script, not by how the code is laid out. A my variable is only available to the code up until the nearest enclosing block or subroutine definition; if you call another subroutine from within that one, the second subroutine won't have access to those variables. Local variables declared with local are available to the code inside that block and subroutine and to nested subroutines called from that same subroutine. That variable definition will cascade to any nested subroutines, in much the same way that global variables are available everywhere. Local local variables are available from that subroutine and all the subroutines it calls as well.

Note

In technical terms, my variables are lexically scoped, and local variables are dynamically scoped. But you don't have to know those terms unless you're a computer scientist, or you're trying to impress other computer scientists.


I've put an example of how the scope differences between my and local variables work in the section “Going Deeper,” if you're interested in looking more at local versus my. For the most part, however, local variables are best defined with my, and not local; my variables follow the more common definition of local scope in other languages and are easier to use and manage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset