Perl for Newbies - Part 3 - Modules and ObjectsPerl ModulesDeclaring a Package

3.1. Declaring a Package

In order to designate that a code belongs to a different namespace you should use the package directive. For instance, if you want your module name to be "MyModule" your file should look something like this:

# This is the file MyModule.pm
#

package MyModule;

use strict;
use warnings;

sub a_function
{
    print "Hello, World!\n";
}

1;

Note that a module has to return a true value to the perl interpreter, which explains the use of "1;".

A namespace may contain sub-namespaces. To separate namespace components, use the :: separator. For example:

# This is the file Hoola/Hoop.pm
#

package Hoola::Hoop;

use strict;
use warnings;

my $counter = 0;

sub get_counter
{
    return $counter++;
}

1;

Written by Shlomi Fish