2. Making your own module

2.1 Create

Creating your own module is quite easy. Recall that a module is a set of functions in a separate file, that can be invoked in your program. Let's make a module with functions that perform four basic math operations (add, subtract, multiply and divide) on two numbers.
package myCalculator;
---------------------------------------------
# BEGIN block
BEGIN
{
print("Welcome to myCalculator, that starts the calculations now\n\n");
}
---------------------------------------------
# END block
END
{
print("\n\nAll calculations done!\n");
}
---------------------------------------------
# Defining subroutines
sub add
{
my($x, $y) = @_;
print("$x + $y = @{[$x + $y]}\n");
}
---------------------------------------------
sub diff
{
my($x, $y) = @_;
print("$x - $y = @{[$x - $y]}\n");
}
---------------------------------------------
sub mult
{
my($x, $y) = @_;
print("$x * $y = @{[$x * $y]}\n");
}
---------------------------------------------
sub div
{
my($x, $y) = @_;
print("$x / $y = @{[$x / $y]}\n");
}
1; # return a true value to the interpreter
This code is saved as myCalculator.pm in the directory /home/reinier/perl5/lib/perl5/myCalculator (you'll have your own directory with folders including .pm files). The package "myCalculator" is invoked -as you've seen- by the command 'use':
use myCalculator::myCalculator; # Directory_Name::Module_Name
myCalculator::add(10, 10); # invoke the subroutine 'add'
myCalculator::diff(20, 10);
myCalculator::mult(10, 3);
myCalculator::div(30, 20);

2.2 Create an easier alternative

Don't declare a package name in your library file myCalculator.pm (or rename it to myCalculator.pl) So remove the line package myCalculator; and just load this file with require and everything will be in the current package.
require "/home/reinier/myCalculator.pl"
add(10, 10); # invoke the subroutine 'add'
diff(20, 10);
mult(10, 3);
div(30, 20);

2.3 Create a template

With h2xs, you can make a template for your module. When running after the prompt
$ h2xs -A -X -n Template::myModule
you'll get the file myModule.pm in the folder Template-myModule/lib/Template/ It contains all information to make a module that meets code and documentation standards. However, when following the suggestions of 2.2 than you should remove all lines between use warnings and our $VERSION (and remove the first line 'package ...' line also)! Below the template with striked lines.
package TEMPLATE::myModule;
---------------------------------------------
use 5.036000;
use strict;
use warnings;
---------------------------------------------
require Exporter;
---------------------------------------------
our @ISA = qw(Exporter);
---------------------------------------------
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use TEMPLATE::myModule ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
---------------------------------------------
our %EXPORT_TAGS = ( 'all' => [ qw(

) ] );
---------------------------------------------
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(

);
---------------------------------------------
our $VERSION = '0.01';

# Preloaded methods go here.

1;
---------------------------------------------
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

TEMPLATE::myModule - Perl extension for blah blah blah

=head1 SYNOPSIS

use TEMPLATE::myModule;
blah blah blah

=head1 DESCRIPTION

Stub documentation for TEMPLATE::myModule, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.

=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

reinier, email: reinier@none.xy

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2023 by reinier

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.36.0 or,
at your option, any later version of Perl 5 you may have available.
=cut