Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Namespaces

Perl has a solution to this problem using namespaces, also called packages.

#!/usr/bin/perl
use strict;
use warnings;

package Calc;
use strict;
use warnings;

sub add {
    my $total = 0;
    $total += $_ for (@_);
    return $total;
}


package main;

print Calc::add(3, 4), "\n";

# add(3, 4);
# would die with:
# Undefined subroutine &main::add called
#   at examples\modules\namespaces.pl line 20.