Skip to content

Perl Packages Guide — Namespaces, Symbol Tables, and Scope Control

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Packages Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl packages create isolated namespaces using the package keyword, where variables declared with our are package-scoped and my provides lexical scoping -- with the current package determining where unqualified symbols are stored in the Symbol Table.

Basic Package

# main package (default)
$name = "global";

package MyPackage;
$name = "package variable";  # $MyPackage::name

package Another;
$name = "another variable";  # $Another::name

package main;
print $name;               # "global"
print $MyPackage::name;    # "package variable"

Package Variables

package Config;

# Package variables (accessible outside)
our $debug = 0;
our $log_file = "/var/log/app.log";

# Lexical variables (private to this file)
my $internal_counter = 0;

sub set_debug {
    my ($level) = @_;
    $debug = $level;
}

sub log_message {
    my $msg = shift;
    open(my $fh, ">>", $log_file);
    print $fh "$msg\n";
    close($fh);
}

Package Switch

package MyApp::Utils;

sub trim {
    my $s = shift;
    $s =~ s/^\s+|\s+$//g;
    return $s;
}

package MyApp::Main;

# Call function from another package
my $clean = MyApp::Utils::trim("  hello  ");
print "'$clean'\n";  # 'hello'

Symbol Tables

package Demo;

our $var = 42;
our @arr = (1, 2, 3);
sub func { return "hello" }

# Inspect symbol table
package main;
print $Demo::{var};    # *Demo::var  (typeglob)
print *Demo::var;      # same

# Symbol table is a hash
for my $name (keys %Demo::) {
    print "$name\n";  # var, arr, func, etc.
}

Common Mistakes

1. Using my with package scope

Declaring my $x in a package makes it lexical, not a package variable. Use our $x for package globals.

2. Forgetting to re-declare package

Each package declaration changes the namespace until the next package or end of file.

3. Variable name clashes

Different packages can have variables with the same name without conflict. Always use fully qualified names from other packages.

Practice Questions

1. What is the default package? main. All code is in the main package until another package is declared.

2. What is the difference between our and my? our declares a package variable (visible outside the package). my declares a lexical variable (private to scope).

3. How do you access a variable from another package? Use the fully qualified name: $OtherPackage::variable.

FAQ

{{< faq question="Can I have multiple packages in one file?" >}} Yes. Each package Foo; declaration switches the namespace. This is common in test files and small modules. {{< /faq >}}

{{< faq question="What is a typeglob?" >}} A typeglob (*foo) represents all symbol table entries for a name: scalar ($foo), array (@foo), hash (%foo), subroutine (&foo), filehandle (foo). {{< /faq >}}

{{< faq question="How do I import symbols from another package?" >}} Use use Package qw(func $var). The package must have an import subroutine (Exporter provides one). {{< /faq >}}

What's Next

Now learn about modules in Perl.

Topic Description Link
Modules Creating and using modules {{< ref "14-modules" >}}
OOP Object-oriented Perl {{< ref "15-oop" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro