1. Perl
  2. Syntax
  3. my

our - Package variable declaration

I will explain the declaration of a package variable by our .

Package variable declaration

Use our to declare a package variable.

our $NUM;

A package variable are a global variable that belong to a package(For example, Foo). Package variable have a fully qualified name(For example, $Foo::NUM). You can access to the package variable from anywhere in the program.

Usually the name of a package variable is wrriten by uppercase characters.

Package variable initialization

You can initialize a package variable at the same time as the declaration of the package variable.

our $NUM = 5;

Package variable scope and lifetime

Describes the life and scope of a package variable.

Package variable lifespan

Once a package variable is declared and initialized, its value will continue to exist until the end of the program.

Package variable scope

At the end of the scope, you will not be able to see it without the package name. You can access to it by using the fully qualified name.

package SomePackage;

our $PACKAGE_VAR1;
$PACKAGE_VAR1 = 1;

# our scope
{
  our $PACKAGE_VAR2 = 2;
}
# At the end of scope, you can only access to it with a fully qualified name
$SomePackage::PACKAGE_VAR2;

See below for a detailed explanation of the scope.

Package declaration

In Perl, a package declaration is the same as a namespace declaration in other language. Generally, package names start with an uppercase character as follows.

package SomePackage;

Package variable can also be accessed with fully qualified names.

$SomePackage::PACKAGE_VAR1

See the following articles for more information on the package declaration.

main package

If no package is declared, the package variable declared in the "main" package.

# Belongs to "main package" as "$main::NUM"
our $NUM;

In Perl, all package variables belong to thier own package. If a package variable looks like not to belong to a package, the package variable belongs to the main package.

For example, predefined variables belong to the "CORE" package.

Should I use package variable declarations?

If you write your own program, you rarely have the opportunity to use our variable declarations. The safer and faster "my" variable declaration makes it possible to write a enough program. Avoid "our" as much as possible and use "my" in most places.

Because my has a lexical scope and a small accessible range, you can write more safety program. Access speed to the variable is also faster.

See the following article for variable declaration by my.

Example

This is an example using a package variable.

use strict;
use warnings;

# Package variable
print "1: Package variable has a qualified name (SomePackage)".
    "It can be referenced from anywhere in the program (even from outside the scope).\n";
{
  # PACKAGE_VAR1 belonging to SomePackage
  $SomePackage::PACKAGE_VAR1 = 1;
  
  # PACKAGE_VAR1 belonging to OtherPackage;
  $OtherPackage::PACKAGE_VAR1 = 2;
}

print "\$SomePackage::PACKAGE_VAR1 = $SomePackage::PACKAGE_VAR1\n";
print "\$OtherPackage::PACKAGE_VAR1 = $OtherPackage::PACKAGE_VAR1\n\n";

Output

1: Package variable has a qualified name (SomePackage)It can be referenced from anywhere in the program (even from outside the scope).
$SomePackage::PACKAGE_VAR1 = 1
$OtherPackage::PACKAGE_VAR1 = 2

Related Informatrion