<P>They work somewhat like compiler directives (pragmata) in that they
tend to affect the compilation of your program, and thus will usually
work well only when used within a <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A>, or <A HREF="../../lib/Pod/perlfunc.html#item_no"><CODE>no</CODE></A>. Most of these
are lexically scoped, so an inner BLOCK may countermand them
by saying:</P>
<PRE>
no integer;
no strict 'refs';
no warnings;</PRE>
<P>which lasts until the end of that BLOCK.</P>
<P>Some pragmas are lexically scoped--typically those that affect the
<CODE>$^H</CODE> hints variable. Others affect the current package instead,
like <CODE>use vars</CODE> and <CODE>use subs</CODE>, which allow you to predeclare a
variables or subroutines within a particular <EM>file</EM> rather than
just a block. Such declarations are effective for the entire file
for which they were declared. You cannot rescind them with <CODE>no
vars</CODE> or <CODE>no subs</CODE>.</P>
<P>The following pragmas are defined (and have their own documentation).</P>
<LI><STRONG><A NAME="item_Interfaces_to_%2F_Emulations_of_Other_Programming_">Interfaces to / Emulations of Other Programming Languages</A></STRONG><BR>
<LI><STRONG><A NAME="item_Locking">File Names, File Systems and File Locking (see also File Handles)</A></STRONG><BR>
<LI><STRONG><A NAME="item_String_Processing%2C_Language_Text_Processing%2C_P">String Processing, Language Text Processing, Parsing, and Searching</A></STRONG><BR>
<LI><STRONG><A NAME="item_Option%2C_Argument%2C_Parameter%2C_and_Configurati">Option, Argument, Parameter, and Configuration File Processing</A></STRONG><BR>
<LI><STRONG><A NAME="item_Internationalization_and_Locale">Internationalization and Locale</A></STRONG><BR>
<LI><STRONG><A NAME="item_Authentication%2C_Security%2C_and_Encryption">Authentication, Security, and Encryption</A></STRONG><BR>
<LI><STRONG><A NAME="item_Server_and_Daemon_Utilities">Server and Daemon Utilities</A></STRONG><BR>
<LI><STRONG><A NAME="item_Archiving_and_Compression">Archiving and Compression</A></STRONG><BR>
<LI><STRONG><A NAME="item_Images%2C_Pixmap_and_Bitmap_Manipulation%2C_Drawin">Images, Pixmap and Bitmap Manipulation, Drawing, and Graphing</A></STRONG><BR>
<LI><STRONG><A NAME="item_Mail_and_Usenet_News">Mail and Usenet News</A></STRONG><BR>
<LI><STRONG><A NAME="item_Utilities">Control Flow Utilities (callbacks and exceptions etc)</A></STRONG><BR>
<LI><STRONG><A NAME="item_File_Handle_and_Input%2FOutput_Stream_Utilities">File Handle and Input/Output Stream Utilities</A></STRONG><BR>
see <A HREF="http://www.perl.com/perl/CPAN/SITES">http://www.perl.com/perl/CPAN/SITES</A> or <A HREF="ftp://www.perl.com/CPAN/SITES">ftp://www.perl.com/CPAN/SITES</A> .</P>
<P>
<HR>
<H1><A NAME="modules: creation, use, and abuse">Modules: Creation, Use, and Abuse</A></H1>
<P>(The following section is borrowed directly from Tim Bunce's modules
file, available at your nearest CPAN site.)</P>
<P>Perl implements a class using a package, but the presence of a
package doesn't imply the presence of a class. A package is just a
namespace. A class is a package that provides subroutines that can be
used as methods. A method is just a subroutine that expects, as its
first argument, either the name of a package (for ``static'' methods),
or a reference to something (for ``virtual'' methods).</P>
<P>A module is a file that (by convention) provides a class of the same
name (sans the .pm), plus an import method in that class that can be
called to fetch exported symbols. This module may implement some of
its methods by loading dynamic C or C++ objects, but that should be
totally transparent to the user of the module. Likewise, the module
might set up an AUTOLOAD function to slurp in subroutine definitions on
demand, but this is also transparent. Only the <EM>.pm</EM> file is required to
exist. See <A HREF="../../lib/Pod/perlsub.html">the perlsub manpage</A>, <A HREF="../../lib/Pod/perltoot.html">the perltoot manpage</A>, and <A HREF="../../lib/AutoLoader.html">the AutoLoader manpage</A> for details about
the AUTOLOAD mechanism.</P>
<P>
<H2><A NAME="guidelines for module creation">Guidelines for Module Creation</A></H2>
<DL>
<DT><STRONG><A NAME="item_Do_similar_modules_already_exist_in_some_form%3F">Do similar modules already exist in some form?</A></STRONG><BR>
<DD>
If so, please try to reuse the existing modules either in whole or
by inheriting useful features into a new class. If this is not
practical try to get together with the module authors to work on
extending or enhancing the functionality of the existing modules.
A perfect example is the plethora of packages in perl4 for dealing
with command line options.
<P>If you are writing a module to expand an already existing set of
modules, please coordinate with the author of the package. It
helps if you follow the same naming scheme and module interaction
scheme as the original author.</P>
<P></P>
<DT><STRONG><A NAME="item_Try_to_design_the_new_module_to_be_easy_to_extend_">Try to design the new module to be easy to extend and reuse.</A></STRONG><BR>
<DD>
Try to <CODE>use warnings;</CODE> (or <A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>use warnings qw(...);</CODE></A>).
Remember that you can add <A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>no warnings qw(...);</CODE></A> to individual blocks
of code that need less warnings.
<P>Use blessed references. Use the two argument form of bless to bless
into the class name given as the first parameter of the constructor,
e.g.,:</P>
<PRE>
sub new {
my $class = shift;
return bless {}, $class;
}</PRE>
<P>or even this if you'd like it to be used as either a static
or a virtual method.</P>
<PRE>
sub new {
my $self = shift;
my $class = ref($self) || $self;
return bless {}, $class;
}</PRE>
<P>Pass arrays as references so more parameters can be added later
(it's also faster). Convert functions into methods where
appropriate. Split large methods into smaller more flexible ones.
Inherit methods from other modules if appropriate.</P>
<P>Avoid class name tests like: <CODE>die "Invalid" unless ref $ref eq 'FOO'</CODE>.
Generally you can delete the <CODE>eq 'FOO'</CODE> part with no harm at all.
Let the objects look after themselves! Generally, avoid hard-wired
class names as far as possible.</P>
<P>Avoid <CODE>$r->Class::func()</CODE> where using <A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>@ISA=qw(... Class ...)</CODE></A> and
<CODE>$r->func()</CODE> would work (see <A HREF="../../lib/Pod/perlbot.html">the perlbot manpage</A> for more details).</P>
<P>Use autosplit so little used or newly added functions won't be a
burden to programs that don't use them. Add test functions to
the module after __END__ either using AutoSplit or by saying:</P>
<PRE>
eval join('',<main::DATA>) || die $@ unless caller();</PRE>
<P>Does your module pass the 'empty subclass' test? If you say
<A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>@SUBCLASS::ISA = qw(YOURCLASS);</CODE></A> your applications should be able
to use SUBCLASS in exactly the same way as YOURCLASS. For example,
does your application still work if you change: <CODE>$obj = new YOURCLASS;</CODE>
into: <CODE>$obj = new SUBCLASS;</CODE> ?</P>
<P>Avoid keeping any state information in your packages. It makes it
difficult for multiple other packages to use yours. Keep state
information in objects.</P>
<P>Always use <STRONG>-w</STRONG>.</P>
<P>Try to <CODE>use strict;</CODE> (or <A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>use strict qw(...);</CODE></A>).
Remember that you can add <A HREF="../../lib/Pod/perlfunc.html#item_qw"><CODE>no strict qw(...);</CODE></A> to individual blocks
of code that need less strictness.</P>
<P>Always use <STRONG>-w</STRONG>.</P>
<P>Follow the guidelines in the <CODE>perlstyle(1)</CODE> manual.</P>
The perlstyle manual supplied with Perl has many helpful points.
<P>Coding style is a matter of personal taste. Many people evolve their
style over several years as they learn what helps them write and
maintain good code. Here's one set of assorted suggestions that
seem to be widely used by experienced developers:</P>
<P>Use underscores to separate words. It is generally easier to read
$var_names_like_this than $VarNamesLikeThis, especially for
non-native speakers of English. It's also a simple rule that works
consistently with VAR_NAMES_LIKE_THIS.</P>
<P>Package/Module names are an exception to this rule. Perl informally
reserves lowercase module names for 'pragma' modules like integer
and strict. Other modules normally begin with a capital letter and
use mixed case with no underscores (need to be short and portable).</P>
<P>You may find it helpful to use letter case to indicate the scope
or nature of a variable. For example:</P>
<PRE>
$ALL_CAPS_HERE constants only (beware clashes with Perl vars)
$Some_Caps_Here package-wide global/static
$no_caps_here function scope my() or local() variables</PRE>
<P>Function and method names seem to work best as all lowercase.
e.g., <CODE>$obj->as_string()</CODE>.</P>
<P>You can use a leading underscore to indicate that a variable or
function should not be used outside the package that defined it.</P>
<P></P>
<DT><STRONG><A NAME="item_Select_what_to_export%2E">Select what to export.</A></STRONG><BR>
<DD>
Do NOT export method names!
<P>Do NOT export anything else by default without a good reason!</P>
<P>Exports pollute the namespace of the module user. If you must
export try to use @EXPORT_OK in preference to @EXPORT and avoid
short or common names to reduce the risk of name clashes.</P>
<P>Generally anything not exported is still accessible from outside the
module using the ModuleName::item_name (or <CODE>$blessed_ref->method</CODE>)
syntax. By convention you can use a leading underscore on names to
indicate informally that they are 'internal' and not for public use.</P>
<P>(It is actually possible to get private functions by saying:
<CODE>my $subref = sub { ... }; &$subref;</CODE>. But there's no way to call that
directly as a method, because a method must have a name in the symbol
table.)</P>
<P>As a general rule, if the module is trying to be object oriented
then export nothing. If it's just a collection of functions then
@EXPORT_OK anything but use @EXPORT with caution.</P>
<P></P>
<DT><STRONG><A NAME="item_Select_a_name_for_the_module%2E">Select a name for the module.</A></STRONG><BR>
<DD>
This name should be as descriptive, accurate, and complete as
possible. Avoid any risk of ambiguity. Always try to use two or
more whole words. Generally the name should reflect what is special
about what the module does rather than how it does it. Please use
nested module names to group informally or categorize a module.
There should be a very good reason for a module not to have a nested name.
Module names should begin with a capital letter.
<P>Having 57 modules all called Sort will not make life easy for anyone
(though having 23 called Sort::Quick is only marginally better :-).
Imagine someone trying to install your module alongside many others.
If in any doubt ask for suggestions in comp.lang.perl.misc.</P>
<P>If you are developing a suite of related modules/classes it's good
practice to use nested classes with a common prefix as this will
avoid namespace clashes. For example: Xyz::Control, Xyz::View,
Xyz::Model etc. Use the modules in this list as a naming guide.</P>
<P>If adding a new module to a set, follow the original author's
standards for naming modules and the interface to methods in
those modules.</P>
<P>To be portable each component of a module name should be limited to
11 characters. If it might be used on MS-DOS then try to ensure each is
unique in the first 8 characters. Nested modules make this easier.</P>
<P></P>
<DT><STRONG><A NAME="item_Have_you_got_it_right%3F">Have you got it right?</A></STRONG><BR>
<DD>
How do you know that you've made the right decisions? Have you
picked an interface design that will cause problems later? Have
you picked the most appropriate name? Do you have any questions?
<P>The best way to know for sure, and pick up many helpful suggestions,
is to ask someone who knows. Comp.lang.perl.misc is read by just about
all the people who develop modules and it's the best place to ask.</P>
<P>All you need to do is post a short summary of the module, its
purpose and interfaces. A few lines on each of the main methods is
probably enough. (If you post the whole module it might be ignored
by busy people - generally the very people you want to read it!)</P>
<P>Don't worry about posting if you can't say when the module will be
ready - just say so in the message. It might be worth inviting
others to help you, they may be able to complete it for you!</P>
<P></P>
<DT><STRONG><A NAME="item_README_and_other_Additional_Files%2E">README and other Additional Files.</A></STRONG><BR>
<DD>
It's well known that software developers usually fully document the
software they write. If, however, the world is in urgent need of
your software and there is not enough time to write the full
documentation please at least provide a README file containing:
<UL>
<LI><STRONG><A NAME="item_A_description_of_the_module%2Fpackage%2Fextension_">A description of the module/package/extension etc.</A></STRONG><BR>
<LI><STRONG><A NAME="item_A_copyright_notice_%2D_see_below%2E">A copyright notice - see below.</A></STRONG><BR>
<LI><STRONG><A NAME="item_Prerequisites_%2D_what_else_you_may_need_to_have%2">Prerequisites - what else you may need to have.</A></STRONG><BR>
<LI><STRONG><A NAME="item_How_to_build_it_%2D_possible_changes_to_Makefile%2">How to build it - possible changes to Makefile.PL etc.</A></STRONG><BR>
<LI><STRONG><A NAME="item_How_to_install_it%2E">How to install it.</A></STRONG><BR>
<LI><STRONG><A NAME="item_Recent_changes_in_this_release%2C_especially_incom">Recent changes in this release, especially incompatibilities</A></STRONG><BR>
<LI><STRONG><A NAME="item_Changes_%2F_enhancements_you_plan_to_make_in_the_f">Changes / enhancements you plan to make in the future.</A></STRONG><BR>
</UL>
<P>If the README file seems to be getting too large you may wish to
split out some of the sections into separate files: INSTALL,
Copying, ToDo etc.</P>
<DL>
<DT><STRONG><A NAME="item_Adding_a_Copyright_Notice%2E">Adding a Copyright Notice.</A></STRONG><BR>
<DD>
How you choose to license your work is a personal decision.
The general mechanism is to assert your Copyright and then make
a declaration of how others may copy/use/modify your work.
<P>Perl, for example, is supplied with two types of licence: The GNU
GPL and The Artistic Licence (see the files README, Copying, and
Artistic). Larry has good reasons for NOT just using the GNU GPL.</P>
<P>My personal recommendation, out of respect for Larry, Perl, and the
Perl community at large is to state something simply like:</P>
<PRE>
Copyright (c) 1995 Your Name. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.</PRE>
<P>This statement should at least appear in the README file. You may
also wish to include it in a Copying file and your source files.
Remember to include the other words in addition to the Copyright.</P>
<P></P>
<DT><STRONG><A NAME="item_Give_the_module_a_version%2Fissue%2Frelease_number">Give the module a version/issue/release number.</A></STRONG><BR>
<DD>
To be fully compatible with the Exporter and MakeMaker modules you
should store your module's version number in a non-my package
variable called $VERSION. This should be a floating point
number with at least two digits after the decimal (i.e., hundredths,
e.g, <CODE>$VERSION = "0.01"</CODE>). Don't use a ``1.3.2'' style version.
See <A HREF="../../lib/Exporter.html">the Exporter manpage</A> for details.
<P>It may be handy to add a function or method to retrieve the number.
Use the number in announcements and archive file names when
releasing the module (ModuleName-1.02.tar.Z).
See perldoc ExtUtils::MakeMaker.pm for details.</P>
<P></P>
<DT><STRONG><A NAME="item_How_to_release_and_distribute_a_module%2E">How to release and distribute a module.</A></STRONG><BR>
<DD>
It's good idea to post an announcement of the availability of your
module (or the module itself if small) to the comp.lang.perl.announce
Usenet newsgroup. This will at least ensure very wide once-off
distribution.
<P>If possible, register the module with CPAN. You should
include details of its location in your announcement.</P>
<P>Some notes about ftp archives: Please use a long descriptive file
name that includes the version number. Most incoming directories
will not be readable/listable, i.e., you won't be able to see your
file after uploading it. Remember to send your email notification
message as soon as possible after uploading else your file may get
deleted automatically. Allow time for the file to be processed
and/or check the file has been processed before announcing its
<P>By using the WWW interface you can ask the Upload Server to mirror
your modules from your ftp or WWW site into your own directory on
CPAN!</P>
<P>Please remember to send me an updated entry for the Module list!</P>
<P></P>
<DT><STRONG><A NAME="item_Take_care_when_changing_a_released_module%2E">Take care when changing a released module.</A></STRONG><BR>
<DD>
Always strive to remain compatible with previous released versions.
Otherwise try to add a mechanism to revert to the
old behavior if people rely on it. Document incompatible changes.
<P></P></DL>
</DL>
<P>
<H2><A NAME="guidelines for converting perl 4 library scripts into modules">Guidelines for Converting Perl 4 Library Scripts into Modules</A></H2>
<DL>
<DT><STRONG><A NAME="item_There_is_no_requirement_to_convert_anything%2E">There is no requirement to convert anything.</A></STRONG><BR>
<DD>
If it ain't broke, don't fix it! Perl 4 library scripts should
continue to work with no problems. You may need to make some minor
changes (like escaping non-array @'s in double quoted strings) but
there is no need to convert a .pl file into a Module for just that.
<P></P>
<DT><STRONG><A NAME="item_Consider_the_implications%2E">Consider the implications.</A></STRONG><BR>
<DD>
All Perl applications that make use of the script will need to
be changed (slightly) if the script is converted into a module. Is
it worth it unless you plan to make other changes at the same time?
<P></P>
<DT><STRONG><A NAME="item_Make_the_most_of_the_opportunity%2E">Make the most of the opportunity.</A></STRONG><BR>
<DD>
If you are going to convert the script to a module you can use the
opportunity to redesign the interface. The guidelines for module
creation above include many of the issues you should consider.
<P></P>
<DT><STRONG><A NAME="item_The_pl2pm_utility_will_get_you_started%2E">The pl2pm utility will get you started.</A></STRONG><BR>
<DD>
This utility will read *.pl files (given as parameters) and write
corresponding *.pm files. The pl2pm utilities does the following:
<UL>
<LI><STRONG><A NAME="item_Adds_the_standard_Module_prologue_lines">Adds the standard Module prologue lines</A></STRONG><BR>
<LI><STRONG><A NAME="item_Converts_package_specifiers_from_%27_to_%3A%3A">Converts package specifiers from ' to ::</A></STRONG><BR>
<LI><STRONG><A NAME="item_die">Converts <CODE>die(...)</CODE> to <CODE>croak(...)</CODE></A></STRONG><BR>
<LI><STRONG><A NAME="item_Several_other_minor_changes">Several other minor changes</A></STRONG><BR>
</UL>
<P>Being a mechanical process pl2pm is not bullet proof. The converted
code will need careful checking, especially any package statements.
Don't delete the original .pl file till the new .pm one works!</P>
</DL>
<P>
<H2><A NAME="guidelines for reusing application code">Guidelines for Reusing Application Code</A></H2>
<DL>
<DT><STRONG><A NAME="item_Complete_applications_rarely_belong_in_the_Perl_Mo">Complete applications rarely belong in the Perl Module Library.</A></STRONG><BR>
<DD>
<DT><STRONG><A NAME="item_Many_applications_contain_some_Perl_code_that_coul">Many applications contain some Perl code that could be reused.</A></STRONG><BR>
<DD>
Help save the world! Share your code in a form that makes it easy
to reuse.
<P></P>
<DT><STRONG><A NAME="item_Break%2Dout_the_reusable_code_into_one_or_more_sep">Break-out the reusable code into one or more separate module files.</A></STRONG><BR>
<DD>
<DT><STRONG><A NAME="item_Take_the_opportunity_to_reconsider_and_redesign_th">Take the opportunity to reconsider and redesign the interfaces.</A></STRONG><BR>
<DD>
<DT><STRONG><A NAME="item_In_some_cases_the_%27application%27_can_then_be_re">In some cases the 'application' can then be reduced to a small</A></STRONG><BR>
<DD>
fragment of code built on top of the reusable modules. In these cases
the application could invoked as:
<PRE>
% perl -e 'use Module::Name; method(@ARGV)' ...
or
% perl -mModule::Name ... (in perl5.002 or higher)</PRE>
<P></P></DL>
<P>
<HR>
<H1><A NAME="note">NOTE</A></H1>
<P>Perl does not enforce private and public parts of its modules as you may
have been used to in other languages like C++, Ada, or Modula-17. Perl
doesn't have an infatuation with enforced privacy. It would prefer
that you stayed out of its living room because you weren't invited, not
because it has a shotgun.</P>
<P>The module and its user have a contract, part of which is common law,
and part of which is ``written''. Part of the common law contract is
that a module doesn't pollute any namespace it wasn't asked to. The
written contract for the module (A.K.A. documentation) may make other
provisions. But then you know when you <CODE>use RedefineTheWorld</CODE> that
you're redefining the world and willing to take the consequences.</P>