Thursday, June 19, 2014

What is a Module


What is a Module

  • Has a filename ending with '.pm' and a package name to match
  • If the filename is in a lower directory, the package name uses '::' for the directory separator
The Barest of Modules

  • package gives the name of the module
  • The filename would be Sample/Module.pm and the Sample directory will live in one of the directories listed in @INC
use Exporter

  • The Exporter module makes the changes to symbol tables to export functions and variables from one package to another
use vars

  • These are package variables needed to make Exporter happy
  • $VERSION is also known as $Sample::Module::VERSION
$VERSION

  • Required variable
  • Should be a real number like 0.1 or 2.9996
  • Should not be like LINUX kernal numbering 2.2.4 with extra periods
@ISA

  • Modules to inherit variables, methods, and functions from
@EXPORT


 


 

  • Functions and Variables here are exported by default. They will pollute the name space of the package that calls this module and can be called without a package name
  • Give a Hoot, Don't Pollute. Only export by default those things that really need it.
@EXPORT_OK

  • Functions and Variables here are exported only if requested.
  • Otherwise they can still be used, but you must give the full name like $Sample::Module::Foo
  • my variables can not be exported, must use vars
%EXPORT_TAGS


 


 

  • Pre-defined groups of things to export. All functions and variables included must also be in either @EXPORT or @EXPORT_OK.
@EXPORT_FAIL


 


 

  • List of things to never ever export
1

  • All modules (and required files) need to return a true value. The simplest way to guarantee a true value is to end the module with a "1;", which is true.
__END__

The "__END__" tells Perl to ignore anything after it. If you have documentation or retired functions you can't yet throw away, it is easy to leave them after the "__END__" where they will do no harm.

No comments: