Simple Module Tutorial
What is @ISA array in perl?
Exporting symbols
Creating Perl Module
=================================================
# cited from the book "Perl Cookbook"
One of the differences between require and use is that use performs
an implict import on the included module's package. An imported symbol
no longer have to use the fully qualified by package name (or declared
with our or the oder use vars if a variable, or with use subs if a
subroutine).You can use importe dviarables as though they were part of
your package. If you imported $English::OUTPUT_AUTOFLUSH in the current
package, you could refer to it as $OUTPUT_AUTOFLUSH.
If
the module name itself contains any double colons, these are translated
into your system's directory separator. That means that the file
File::Find module resides in the file File/Find.pm under most file
systems.
# The following 2 lines need to be enclosed in each module.
use Exporter;
@ISA=("Exporter");
The
Exporter module manages the module's public interface. The special,
per-package array @ISA was initialized to contain the word "Exporter".
When
a user says use Cards::Poker, Perl implicitly calls a special method,
Cards::Poker->import( ). You don't have an import method in your
package, but that's ok, because the Exporter package does, and you are
inheriting it from it, because of the assignment to @ISA. Perl look at
the package's @ISA for resolution of undefined methods.
@EXPORT = qw (&shuffle @card_deck);
The list ('&shuffle', '@card_deck') was assigned to the special, per-package array @EXPORT. When someone imports this module, variables and functions listed in that array are aliased into the caller's own package. That way they don't have to call the function Cards::Poke::shuffle(23) after the import. They can just write shuffle(23) instead. This won't happen if they load Cards::Poker with require Cards::Poker; only a use imports.
If the last evaluated expression in the module does not produce a true value, an exception will be raised. Therefore, the last of a module is a simple 1.
A library is a collection of loosely related functions designed to be used by other programs. It lacks the rigorous semantics of a Perl module. The file extension .pl indicates that it is a Perl library file.
Perl libraries -- or in fact, any arbitrary file with Perl code in it -- can be loaded in using do "file.pl" or with require "file.pl". The latter is preferred in most situations, because unlike do, require does implicit error checking.
A .ph is used for C header files that have been translated into Perl libraries using the h2ph tool. A .xs indicate an augmented C source file, possibly created by the h2xs tool, which will be complied by the xsubpp tool and your C compiler into native machine code.
An object-orientated module seldom uses the import-export mechanism at all. Instead, it provides an object-orientated interface full of constructors, destructors, methods, inheritance, and operator overloading.
No comments:
Post a Comment