Home arrow Perl Programming arrow Page 2 - Understanding Scope and Packages in Perl

Introduction to Packages - 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



When we start programming, weíre in a package called main. A package is a collection of variables that is separate from another package. Letís say we have two packages: A and B. Each package can have its own variable named$x, and those two$xvariables are completely distinct.

If we assign$x, as we did previously inglobal2.pl, then we create a package variable$xin packagemain (themainpackage is the default package). Perl knows it by its full name,$main::xóthe variable$xin themainpackageóbut because weíre in themain package when we make the assignment, we can just call it by its short name,$x. Itís like the phone systemó you donít have to dial the area code when you call someone in the same area as you.1

1. Depending on your location, of course. Nowadays, with so many area codes in a metropolitan area, to call across the street often requires dialing 10 digits . . .

We can create a variable in another package by using a fully qualified name. Instead of the main package, we can have a package calledFred. Here weíll store all of Fredís variables and subroutines. So, to get at the$namevariable in packageFred, we say$Fred::name, like this:

$x = 10;
$Fred::name = "Fred Flintstone";

The fact that itís in a different package doesnít mean we canít get at it. Remember that these are global variables, available from anywhere in our program. All packages do is give us a way of subdividing the namespace.

What do we mean by ďsubdividing the namespaceĒ? Well, the namespace is the set of names we can give our variables. Without packages, we could only have one$name. What packages do is help us make$namein packageFreddifferent to$namein packageBarneyand$namein packagemain.

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

$main::name = "Your Name Here"; $Fred::name = "Fred Flintstone"; $Barney::name = "Barney Rubble";

print "\$name in package main is $name\n"; print "\$name in package Fred is $Fred::name\n";
print "\$name in package Barney is $Barney::name\n";

$ perl globals1.pl
$name in package main is Your Name Here $name in package Fred is Fred Flintstone $name in package Barney is Barney Rubble

You can change what package youíre currently working in with the aptly namedpackageoperator. We could write the preceding like this:

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

$main::name = "Your Name Here";
$Fred::name = "Fred Flintstone"; $Barney::name = "Barney Rubble";

print "\$name in package main is $name\n";
package Fred;
print "\$name in package Fred is $name\n";
package Barney;
print "\$name in package Barney is $name\n";
package main;

Whenuse strict is in force, it makes us use the full names for our package variables. If we try and say this:

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

use strict;

$x = 10;
print $x;

Perl will give us an erroróglobal symbol$xrequires an explicit package name. The package name itís looking for ismain, and it wants us to say$main::x.

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

use strict;

$main::x = 10;
print $main::x, "\n";

Global variables can be accessed and altered at any time by any subroutine or assignment that you care to apply to them. Of course, this is handy if you want to store a valueófor instance, the userís nameóand be able to get it anywhere.

Itís also an absolute pain in the neck when it comes to subroutines. Hereís why:

$a = 25;
$b = some_sub(10);
print $a;

Looks innocent, doesnít it? Looks like we should see the answer 25. But what happens ifsome_sub()uses and changes the global$a? Any variable anywhere in your program can be wiped out by another part of your programówe call this action at a distance, and it gets real spooky to debug. Packages alleviate the problem, but to make sure that we never get into this mess, we have to ensure that every variable in our program has a different name. In small programs, thatís feasible, but in huge team efforts, itís a nightmare. Itís far clearer to be able to restrict the possible effect of a variable to a certain area of code, and thatís exactly what lexical variables do.

>>> 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: