perl: define a module to be called in another script

First, define a module. The module must be in a path reachable by perl (e.g., local directory). Then, to make a script a valid package, you need to proceed as follows:

  • Rename the perl script with the extension .pm
  • It must return True. Just add 1; on the last line
  • Give a name to the package by adding package CLEVERNAME;on the top

The name of the file is the name given to the package internally (CLEVERNAME). The package is now ready. For instance:

#!/usr/bin/perl -w
package CLEVERNAME;
sub my_function {
    print STDERR "It works\n";
}
1;

On the script side, create a perl script and use the use command as follows (assuming there is a function called my_function in your package:

#!/usr/bin/perl -w
 
use CLEVERNAME;
 
# call you function 
CLEVERNAME::my_function;
Please follow and like us:
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.