Perl Modules

Using and creating Perl modules

Using Modules

use strict; # load module at compile time
use warnings;
use Data::Dumper; # external module
require MyModule; # load at runtime

Importing Functions

use List::Util qw(max min sum); # import specific functions
use File::Basename 'basename'; # import one function

Creating a Module

package MyModule; # declare package
use strict;
use warnings;
sub hello {
    print "Hello from module\n";
}
1; # module must return true

Exporting Functions

package MyModule;
use Exporter qw(import); # use Exporter
our @EXPORT = qw(hello); # export by default
our @EXPORT_OK = qw(goodbye); # export on request

Popular Modules

DBI # database interface
LWP::UserAgent # HTTP client
JSON # JSON encoding/decoding
DateTime # date/time handling
File::Path # file operations