Broad Network

Namespace in Perl

Perl Course

Foreword: In this tutorial, I talk about the topic, Namespace in Perl. Namespace in a computer language solves name conflict and name ambiguity.

By: Chrysanthus Date Published: 8 Nov 2015


In this tutorial, I talk about the topic, Namespace in Perl. Namespace in a computer language solves name conflict and name ambiguity. I elaborate on all that below.

This tutorial is part of the Volume, Perl Course. At the bottom of this page, you will find links to the different series you should have read before reaching here.

A problem
Try the following code:

use strict;

    my $myInt = 3;
    my $yourInt = 5;

    my $herInt = $myInt + $yourInt;

The execution of the above code went well. The script (program) has no output. In the code, you have the initialization of two integers with two variables. You have one addition statement, where a third variable is defined.

Now, read and try the following code where another variable is defined. Two variables have the same name, $myInt.

use strict;

    my $myInt = 4;

    my $myInt = 3;

    my $yourInt = 5;

    my $herInt = $myInt + $yourInt;

    print $myInt, "\n";
    print $herInt;

The output is:


The output shows that it is the second declared variable ($myInt) that took effect. That is ambiguous! Why was the value of the first variable not used in the addition? The reason for this error is because you cannot have more than one variable of the same type with the same name in the same scope. There are three common variable types in Perl, which are scalar, array and hash. The scope in question here is the file scope (main program).

Normally, if you write a one-file program you would probably be conscious not to have two variables (of the same type) with the same name in the same scope. However, the problem can arise because many programs (applications) are large and can be written by different people; each person may write a file. Assume that you have one main file and modules. The modules are incorporated into the main file using the use or require command.

When talking about namespaces, we are talking about variables, in a way that you should use them.

To solve the above name ambiguity (conflict) with one file or with multiple files, the developers of Perl came up with the idea of namespaces. It is simple: you are advised to have the variables of your code in a block, or better, a package. That package is a namespace. The package name is the identifier of the package (without the preceding $). The identifier is the name of the namespace. In the two namespaces below, we shall have initialization of variables and definition of functions.

Namespace Examples
Read and try the following code (there is no output):

use strict;

    {package First;
        our $ident = 33;
        sub fn
                #some statements

    {package Second;
        our $ident = 44;
        sub fn
                #some statements

    my $myInt = $First::ident;
    my $yourInt = $Second::ident;


The name of the namespace is the name of the package. A namespace can also take the form of a module (non-block package). The above code is OK. It compiles and is interpreted successfully.

There are two namespaces in the above code: one is called, First and the other is called, Second. Inside First, you have the initialization of the variable, $ident and the definition of the function, fn(). Inside the namespace, Second, you have the initialization of the variable, $ident and the definition of the function, fn(). Note that inside the two namespaces, the variables and the function definitions have the same names.

The Double Colon Operator
Since a namespace is also a package, to access a variable from outside the package, you have to use the double colon operator, :: .

Using a Namespace
In the above code, outside the namespace, in order to access the variable from the namespace, you have to use the double colon operator. You type the name of the variable beginning with $; then you insert the namespace name followed by double colons, between the $ and the text name, e.g.


The next two statements after the namespaces above, initialize two new variables. The first statement uses the integer variable from the namespace, First. The second statement uses the integer variable, from the namespace, Second. Note the use of the double colon operators in these two statements. In the namespaces, the two variables have the same name. Outside the namespaces, they are differentiated with the double colon operator preceded by the namespace names.

To call a function outside the namespace, you begin with the namespace name. This is followed by the double colon; then the name of the function, and possibly arguments.

Namespace and Version
A namespace is a package. In the same way that you specify the package version, you specify the namespace version, as in,

    {package First 2.16;
        #some statements

where 2.16 is the version of the namespace.

The main Namespace
The main package or the main namespace is like the main executable Perl file. The name of the main namespace is, main. Variables and functions defined in the main namespace can be called without “main::”. Actually, to call a variable or a function defined in the main namespace, you should use one of the following syntaxes:




In the case of a variable, precede the syntax with $ or @ or %.

Special Variables and Filehandles
All special variables, e.g. $/ are in the main namespace. The following filehandles can be considered to be in the main namespace: STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC, and SIG.

That is it.


Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course


Become the Writer's Fan
Send the Writer a Message