Perl modules

Definition

A perl module is a piece of perl code reusable by different programs. It helps keeping your code well organized.

The module structure looks as following.

Example of a simple module:

Hello.pmprogram.pl
package Hello;

sub greet {
  return "Hi world!";
}
1;
#!/usr/bin/perl


use Hello;

print Hello::greet();

Syntax explanation

package Hello;  # Declare a package; file must be saved as Hello.pm

sub greet {     # Contents of the package: functions, and variables
  return "Hello world!";
}
1;              # Return a true value at end


Table of Contents.
Next.