WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Intermediate · question 21 of 100

What are Perl’s package, namespace, and scope rules?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

In Perl, a package defines a namespace, which is a named container for variables, functions, and other types of symbols. A package is defined using the ‘package‘ declaration at the beginning of a file or a block of code, and its name is a string that follows the rules for identifiers (alphanumeric characters and underscores).

For example, to define a package called ‘MyModule‘, you would write:

package MyModule;

Any symbols defined in this package will be prefixed with its name, separated by ‘::‘. For instance, a variable ‘$count‘ defined in ‘MyModule‘ would be accessed using the fully-qualified name ‘$MyModule::count‘.

A package can be nested within another package by specifying its fully-qualified name as the argument to the ‘package‘ declaration. For example, to define a package called ‘MyModule::Submodule‘, you would write:

package MyModule::Submodule;

Any symbols defined in this package will be accessed using the fully-qualified name with both package names concatenated by ‘::‘. For instance, a variable ‘$total‘ defined in ‘MyModule::Submodule‘ would be accessed using the fully-qualified name ‘$MyModule::Submodule::total‘.

Perl’s scope rules determine how symbols defined in a package are accessed and when they are destroyed. There are different types of scopes, some of which are package scope, lexical scope, and dynamic scope.

Package scope is the default scope for symbols defined in a package. This means that any variable, function or other symbol defined without a sigil (e.g. ‘$‘, ‘@‘, ‘%‘) and not declared with ‘my‘, ‘our‘, or ‘state‘, has package scope. Package-scoped symbols are visible and accessible from anywhere within the package, as well as from other packages that use the ‘package‘ directive to switch to the same namespace.

Lexical scope is used for symbols declared with ‘my‘. These symbols are only visible and accessible from within the block of code where they are defined, including any nested blocks. This allows for private variables that don’t affect the behavior of other parts of the program.

Dynamic scope is used for symbols declared with ‘local‘. These symbols modify the behavior of built-in functions and are only in effect for the duration of the block of code where they are defined. This allows for fine-grained control over the behavior of Perl’s built-in functionality.

In addition to these basic scope rules, Perl has a number of scoping constructs that are used to control the lifespan and visibility of objects and closures, such as ‘BEGIN‘, ‘END‘, ‘eval‘, and ‘goto‘.

Here’s an example to illustrate these concepts:

package MyModule;

# Package scope
$count = 0;

# Lexical scope
sub add {
    my ($a, $b) = @_;
    my $total = $a + $b;
    $count++;  # Increments the global count variable
    return $total;
}

# Dynamic scope
sub print_count {
    local $count = 42; # Temporarily sets the global count variable to 42
    print "Count is $countn";
}

sub run {
    print add(2, 3), "n";         # Prints "5"
    print_count();                 # Prints "Count is 42"
    print "Count is $countn";     # Prints "Count is 1"
}

1; # End of package MyModule

In this example, ‘MyModule‘ defines a package with a package-scoped variable ‘$count‘, a function ‘add‘ that has a lexical variable ‘$total‘, and a function ‘print_count‘ that has a dynamically-scoped variable ‘$count‘.

When ‘MyModule::run‘ is called, it calls ‘add(2,3)‘ to add two numbers and increments the global ‘$count‘ variable. It then calls ‘print_count()‘, which temporarily sets ‘$count‘ to 42 and prints it, but doesn’t affect the global ‘$count‘ variable. Finally, it prints the global ‘$count‘ variable, which has been incremented by ‘add()‘, yielding a value of 1.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics