When it came time to add modules to Perl, thought was given to how this could be done and still support the old libraries. It was decided that a new directive was needed. Thus, use was born.
The use directive will automatically export function and variable names to the main namespace by calling the module's import() function. Most modules don't have their own import() function; instead they inherit it from the Exporter module. You have to keep in mind that the import() function is not applicable to object-oriented modules. Object-oriented modules should not export any of their functions or variables.
You can use the following lines as a template for creating your own modules, mod_template.pl:
package Module; require(Exporter); @ISA = qw(Exporter); @EXPORT = qw(funcOne $varOne @variable %variable); @EXPORT_OK = qw(funcTwo $varTwo);
The names in the @EXPORT array will always be moved into the main namespace. Those names in the @EXPORT_OK will be moved only if you request them. This small module can be loading into your script using this statement:
use Module;
Since use is a compiler directive, the module is loaded as soon as the compiler sees the directive. This means that the variables and functions from the module are available to the rest of your script.
If you need to access some of the names in the @EXPORT_OK array, use a statement like this:
use Module qw(:DEFAULT funcTwo); # $varTwo is not exported.
Once you add optional elements to the use directive you need to explicitly list all of the names that you want to use. The :DEFAULT is a short way of saying, "give me everything in the @EXPORT list."