Perl logo

Perl Refreshments ⌘

Variables & Scope

Most programming languages have variables of some kind, as well as user-defined functions or subroutines.

Variables may be a tricky concept to understand, but think of them as a trailer that you haul behind a truck.

A trailer is a kind of container, but it is used to hold other things (maybe other containers as well).

Like many trailers, you can put all kinds of things in one - boxes, lumber, skids holding things, a horse or 2, or maybe a roomful of your stuff.

Some trailers though, are designed to specifically carry certain things. Think of the specialty trailers that bring cars to the local dealer. Now imagine trying to carry a dozen cows on that trailer - hmmm.

Or imagine a different one that hauls gasoline or other liquids. That would not work very well trying to move your household belongings to another city.

So a variable in programming is like a trailer - most times you can put whatever you like into them. But there are special kinds of variables designed to hold certain things.

Another way to think of variables is to think of algebra. Algebra deals with variables (c2 = a2 + b2), and we have no problem understanding that a and b may hold any numeric value we wish - thus being variable.

Regardless how you think of variables, in Perl each variable is only 1 of the different data types mentioned here.

Scope

Scope affects where and when you can use a particular variable in your program.

Using our analogy from above, the kind of licence we have for our truck and trailer will determine how we can use it. A scope is a confinement or limitation.

One particular license may allow us to haul flammable liquids. Another may allow us to haul anything, but restrict us to only driving during the day.

These conditions limit (or provide a scope for) where we can use the variable, I mean truck.

In Perl, the scope of a variable depends on where and how you define or declare it.

Now the twist (or pearl): there are 3 scoping declarations!

  1. private (my) lexical scope
    confine both names and values to a scope
  2. global (our) lexical to the end of scope
    confine names to a scope
  3. local (local) dynamic scope
    confine values to a scope

The scope of a variable is generally considered to be 'the innermost enclosing block'.

A block is defined by 1 of 3 things:

  1. enclosed in braces { }
  2. a file
  3. an eval statement

This is perhaps best realized by example...

print "\n" . "=" x20 . " scopes "."=" x20 . "\n";

our $var="boo"; # <-- outside the block
{ # <--- begins a block
     print "our \$var: $var\n";
     my $var="far"; # <-- inside the block
     print "my \$var: $var\n";

} # <--- ends a block
print "\$var: $var\n"; # <-- which one did this print?
print "\n" . "=" x20 . " All Done " . "=" x20 ."\n";

scope 1

We first defined 'our $var' outside the block defined by braces. Then defined 'my $var' inside the block. Printing each one shows how it is affected by the scope.

As you can see, this has a definite affect on where you can use that particular variable in your program.

Generally, a variable declaration is only applicable in the innermost enclosing scope.

In Perl an enclosing scope is either a block of code; a file; or an eval statement - whichever comes first.

Technically, this is known as lexically scoped.

A key feature is that the variable is available only in the innermost block. This means that if you have a subroutine that includes a nested subroutine, variables declared in the innermost block are only available within that block.

Quoting Variables
-30-