Home arrow Perl Programming arrow Understanding Scope and Packages in Perl

Understanding Scope and Packages in Perl

In this fourth part of a six-part article series on subroutines and functions in Perl, you will learn about two different types of variables (global and lexical) and their scopes. You will also learn what a package is. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

  1. Understanding Scope and Packages in Perl
  2. Introduction to Packages
By: Apress Publishing
Rating: starstarstarstarstar / 0
April 23, 2012

print this article



Understanding Scope

It’s now time to have a look at what we’re doing when we declare a variable with my(). The truth, as we’ve briefly glimpsed it, is that Perl has two types of variable. One type is the global variable (or package variable), which can be accessed anywhere in the program, and the second type is the lexical variable (or local variable), which we declare withmy().

Global Variables

All variables in the program are global by default. Consider this code:

#!/usr/bin/perl -w

$x = 10;

$xis a global variable. It is available in every subroutine in the program. For instance, here is a program that accesses a global variable:

#!/usr/bin/perl -w
# global1.pl

$x = 10;


sub access_global {
print "value of \$x: $x\n";

Executing this code shows that$xis accessible inaccess_global():

$ perl global1.pl
value of $x: 10

Since variables within functions are global by default, functions can modify variables as shown in this program:

#!/usr/bin/perl -w
# global2.pl

$x = 10;

print "before: $x\n";
print "after: $x\n";

sub change_global {
$x = 20;
print "in change_global(): $x\n";

This program assigns the global variable$xthe value 10 and then prints that value. Then,change_global()is invoked. It assigns$xthe value 20—this accesses the global variable$x—and then prints its value. Then in the main part of the code, after the function is called, the global$xis printed with its new value—20. Here we see the proof:

$ perl global2.pl
before: 10
in change_global(): 20
after: 20

The fact that Perl function variables are global by default is itself not a bad thing, unless of course you are not expecting it. If you are not expecting it, then accidentally overwriting global variables can cause hard-to-find bugs. If you are expecting it, you will probably want to make function arguments local.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: