Perl Variables                 

You don't have to declare variables specifically in Perl. The first reference to a variable initializes it, scoped in one of three ways.

Global variables are those common to the entire class (called a package in Perl). They are declared without any modifiers:

#!/usr/local/bin/perl
package main;
$myvar = 0; #$myvar is now available to the entire package main.
&do_print;
sub do_print { print "My variable = $myvar\n"; }

Note that the package main is created automagically if you don't specify a package name. Subroutines within the package are referenced by 2 colons. main::do_print refers to the do_print subroutine in the package main. In OOP terminology, the package is a class and a subroutine is a method.

Local variables are declared using the keyword local. Variables declared local are visible everywhere inside the block in which they are declared (dynamically scoped), where a block is any group of statements delineated by curly braces.

#!/usr/local/bin/perl
package main;
$myvar = 0;
&do_print;
sub do_print { #Begin Block
    local $tag = "My variable";
    print "$tag = $myvar\n";
} #End Block
print "Tag = $tag\n";

This script produces the following output:

# ./myscript.pl
My variable = 0
Tag =

Notice that the variable $tag has no value outside the block in which it was declared (in this case the subroutine do_print). A variable that has no value, by the way, is undefined; the defined method can be used to check a variable's status. If you want to undefine a currently defined variable, you can use the undef method.

#!/usr/local/bin/perl
package main;
$myvar = 0;
&do_print;
sub do_print { #Begin Block
    local $tag = "My variable";
    print "$tag = $myvar\n";
} #End Block
print "Tag = $tag\n" if defined($tag);

The output now is reduced to

# ./myscript.pl
My variable = 0

because the last print statement is contingent upon the defined check returning true (1) for $tag, which can't be seen outside of the do_print block. If we take away the local scoping, $tag becomes a global variable and is visible to the last print call:

#!/usr/local/bin/perl
package main;
$myvar = 0;
&do_print;
sub do_print { #Begin Block
    $tag = "My variable";
    print "$tag = $myvar\n";
} #End Block
print "Tag = $tag\n" if defined($tag);

# ./myscript.pl
My variable = 0
Tag = My variable

Private variables are declared using the keyword my. Private variables are visible from the point at which they are declared until the end of their innermost enclosing block; in other words, until the first right curly brace is encountered (lexically scoped).

#!/usr/local/bin/perl
package main;
$myvar = 0;
&do_print;
sub do_print {
    $tag = "My variable";
    print "$tag = $myvar\n";
    for ($i=1; $i<5; $i++) {
        my $sum += $i;
        print "Sum inside the for loop = $sum\n";
    }
    print "Sum outside the for loop = $sum\n";
}

# ./myscript.pl
My variable = 0
Sum inside the for loop = 1
Sum inside the for loop = 2
Sum inside the for loop = 3
Sum inside the for loop = 4
Sum outside the for loop =


Next | Previous | SAPM Home


Created by Robert G. Ferrell