This tutorial is just an introduction to perl module.Here you will find a very simple approach of module creation.
Each module contains a package declaration–usually as the first statement of the file–and this package declaration must match the name of the file itself (the file will additionally have a “.pm” extension; so, in our example above, the module would reside in a file called “MyFirstModule.pm”)

package MyFirstModule;

sub hello_message {
return "Hello Visitors!";
}
1;

Your module must return a true value (or a value synonymous with true) so that Perl knows that it was loaded properly when it is used. This will often be the case without having to do anything special; as many Perl commands will return a value that will be interpreted as true anyway; but to be sure of it we use a common module coding trick: We include, as the last statement of the module, a single “command:”

1;
Perl interprets this expression and of course returns true since 1 is always interpreted as a true value.

Having defined our simple module, let’s have a look at a script that makes use of it:

#!/usr/bin/perl
use MyFirstModule;
print MyFirstModule::hello_message()."\n";

Leave a Reply

Your email address will not be published. Required fields are marked *