Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1159 lines
32 KiB

  1. =head1 NAME
  2. perlmodlib - constructing new Perl modules and finding existing ones
  3. =head1 DESCRIPTION
  4. =head1 THE PERL MODULE LIBRARY
  5. A number of modules are included the Perl distribution. These are
  6. described below, and all end in F<.pm>. You may also discover files in
  7. the library directory that end in either F<.pl> or F<.ph>. These are old
  8. libraries supplied so that old programs that use them still run. The
  9. F<.pl> files will all eventually be converted into standard modules, and
  10. the F<.ph> files made by B<h2ph> will probably end up as extension modules
  11. made by B<h2xs>. (Some F<.ph> values may already be available through the
  12. POSIX module.) The B<pl2pm> file in the distribution may help in your
  13. conversion, but it's just a mechanical process and therefore far from
  14. bulletproof.
  15. =head2 Pragmatic Modules
  16. They work somewhat like pragmas in that they tend to affect the compilation of
  17. your program, and thus will usually work well only when used within a
  18. C<use>, or C<no>. Most of these are lexically scoped, so an inner BLOCK
  19. may countermand any of these by saying:
  20. no integer;
  21. no strict 'refs';
  22. which lasts until the end of that BLOCK.
  23. Unlike the pragmas that effect the C<$^H> hints variable, the C<use
  24. vars> and C<use subs> declarations are not BLOCK-scoped. They allow
  25. you to predeclare a variables or subroutines within a particular
  26. I<file> rather than just a block. Such declarations are effective
  27. for the entire file for which they were declared. You cannot rescind
  28. them with C<no vars> or C<no subs>.
  29. The following pragmas are defined (and have their own documentation).
  30. =over 12
  31. =item use autouse MODULE => qw(sub1 sub2 sub3)
  32. Defers C<require MODULE> until someone calls one of the specified
  33. subroutines (which must be exported by MODULE). This pragma should be
  34. used with caution, and only when necessary.
  35. =item blib
  36. manipulate @INC at compile time to use MakeMaker's uninstalled version
  37. of a package
  38. =item diagnostics
  39. force verbose warning diagnostics
  40. =item integer
  41. compute arithmetic in integer instead of double
  42. =item less
  43. request less of something from the compiler
  44. =item lib
  45. manipulate @INC at compile time
  46. =item locale
  47. use or ignore current locale for builtin operations (see L<perllocale>)
  48. =item ops
  49. restrict named opcodes when compiling or running Perl code
  50. =item overload
  51. overload basic Perl operations
  52. =item re
  53. alter behaviour of regular expressions
  54. =item sigtrap
  55. enable simple signal handling
  56. =item strict
  57. restrict unsafe constructs
  58. =item subs
  59. predeclare sub names
  60. =item vmsish
  61. adopt certain VMS-specific behaviors
  62. =item vars
  63. predeclare global variable names
  64. =back
  65. =head2 Standard Modules
  66. Standard, bundled modules are all expected to behave in a well-defined
  67. manner with respect to namespace pollution because they use the
  68. Exporter module. See their own documentation for details.
  69. =over 12
  70. =item AnyDBM_File
  71. provide framework for multiple DBMs
  72. =item AutoLoader
  73. load functions only on demand
  74. =item AutoSplit
  75. split a package for autoloading
  76. =item Benchmark
  77. benchmark running times of code
  78. =item CPAN
  79. interface to Comprehensive Perl Archive Network
  80. =item CPAN::FirstTime
  81. create a CPAN configuration file
  82. =item CPAN::Nox
  83. run CPAN while avoiding compiled extensions
  84. =item Carp
  85. warn of errors (from perspective of caller)
  86. =item Class::Struct
  87. declare struct-like datatypes
  88. =item Config
  89. access Perl configuration information
  90. =item Cwd
  91. get pathname of current working directory
  92. =item DB_File
  93. access to Berkeley DB
  94. =item Devel::SelfStubber
  95. generate stubs for a SelfLoading module
  96. =item DirHandle
  97. supply object methods for directory handles
  98. =item DynaLoader
  99. dynamically load C libraries into Perl code
  100. =item English
  101. use nice English (or awk) names for ugly punctuation variables
  102. =item Env
  103. import environment variables
  104. =item Exporter
  105. implements default import method for modules
  106. =item ExtUtils::Embed
  107. utilities for embedding Perl in C/C++ applications
  108. =item ExtUtils::Install
  109. install files from here to there
  110. =item ExtUtils::Liblist
  111. determine libraries to use and how to use them
  112. =item ExtUtils::MM_OS2
  113. methods to override Unix behaviour in ExtUtils::MakeMaker
  114. =item ExtUtils::MM_Unix
  115. methods used by ExtUtils::MakeMaker
  116. =item ExtUtils::MM_VMS
  117. methods to override Unix behaviour in ExtUtils::MakeMaker
  118. =item ExtUtils::MakeMaker
  119. create an extension Makefile
  120. =item ExtUtils::Manifest
  121. utilities to write and check a MANIFEST file
  122. =item ExtUtils::Mkbootstrap
  123. make a bootstrap file for use by DynaLoader
  124. =item ExtUtils::Mksymlists
  125. write linker options files for dynamic extension
  126. =item ExtUtils::testlib
  127. add blib/* directories to @INC
  128. =item Fatal
  129. make errors in builtins or Perl functions fatal
  130. =item Fcntl
  131. load the C Fcntl.h defines
  132. =item File::Basename
  133. split a pathname into pieces
  134. =item File::CheckTree
  135. run many filetest checks on a tree
  136. =item File::Compare
  137. compare files or filehandles
  138. =item File::Copy
  139. copy files or filehandles
  140. =item File::Find
  141. traverse a file tree
  142. =item File::Path
  143. create or remove a series of directories
  144. =item File::Spec
  145. portably perform operations on file names
  146. =item File::Spec::Functions
  147. function call interface to File::Spec module
  148. =item File::stat
  149. by-name interface to Perl's builtin stat() functions
  150. =item FileCache
  151. keep more files open than the system permits
  152. =item FileHandle
  153. supply object methods for filehandles
  154. =item FindBin
  155. locate directory of original Perl script
  156. =item GDBM_File
  157. access to the gdbm library
  158. =item Getopt::Long
  159. extended processing of command line options
  160. =item Getopt::Std
  161. process single-character switches with switch clustering
  162. =item I18N::Collate
  163. compare 8-bit scalar data according to the current locale
  164. =item IO
  165. load various IO modules
  166. =item IO::File
  167. supply object methods for filehandles
  168. =item IO::Handle
  169. supply object methods for I/O handles
  170. =item IO::Pipe
  171. supply object methods for pipes
  172. =item IO::Seekable
  173. supply seek based methods for I/O objects
  174. =item IO::Select
  175. OO interface to the select system call
  176. =item IO::Socket
  177. object interface to socket communications
  178. =item IPC::Open2
  179. open a process for both reading and writing
  180. =item IPC::Open3
  181. open a process for reading, writing, and error handling
  182. =item Math::BigFloat
  183. arbitrary length float math package
  184. =item Math::BigInt
  185. arbitrary size integer math package
  186. =item Math::Complex
  187. complex numbers and associated mathematical functions
  188. =item Math::Trig
  189. simple interface to parts of Math::Complex for those who
  190. need trigonometric functions only for real numbers
  191. =item NDBM_File
  192. tied access to ndbm files
  193. =item Net::Ping
  194. Hello, anybody home?
  195. =item Net::hostent
  196. by-name interface to Perl's builtin gethost*() functions
  197. =item Net::netent
  198. by-name interface to Perl's builtin getnet*() functions
  199. =item Net::protoent
  200. by-name interface to Perl's builtin getproto*() functions
  201. =item Net::servent
  202. by-name interface to Perl's builtin getserv*() functions
  203. =item Opcode
  204. disable named opcodes when compiling or running Perl code
  205. =item Pod::Text
  206. convert POD data to formatted ASCII text
  207. =item POSIX
  208. interface to IEEE Standard 1003.1
  209. =item SDBM_File
  210. tied access to sdbm files
  211. =item Safe
  212. compile and execute code in restricted compartments
  213. =item Search::Dict
  214. search for key in dictionary file
  215. =item SelectSaver
  216. save and restore selected file handle
  217. =item SelfLoader
  218. load functions only on demand
  219. =item Shell
  220. run shell commands transparently within Perl
  221. =item Socket
  222. load the C socket.h defines and structure manipulators
  223. =item Symbol
  224. manipulate Perl symbols and their names
  225. =item Sys::Hostname
  226. try every conceivable way to get hostname
  227. =item Sys::Syslog
  228. interface to the Unix syslog(3) calls
  229. =item Term::Cap
  230. termcap interface
  231. =item Term::Complete
  232. word completion module
  233. =item Term::ReadLine
  234. interface to various C<readline> packages
  235. =item Test::Harness
  236. run Perl standard test scripts with statistics
  237. =item Text::Abbrev
  238. create an abbreviation table from a list
  239. =item Text::ParseWords
  240. parse text into an array of tokens
  241. =item Text::Soundex
  242. implementation of the Soundex Algorithm as described by Knuth
  243. =item Text::Tabs
  244. expand and unexpand tabs per the Unix expand(1) and unexpand(1)
  245. =item Text::Wrap
  246. line wrapping to form simple paragraphs
  247. =item Tie::Hash
  248. base class definitions for tied hashes
  249. =item Tie::RefHash
  250. base class definitions for tied hashes with references as keys
  251. =item Tie::Scalar
  252. base class definitions for tied scalars
  253. =item Tie::SubstrHash
  254. fixed-table-size, fixed-key-length hashing
  255. =item Time::Local
  256. efficiently compute time from local and GMT time
  257. =item Time::gmtime
  258. by-name interface to Perl's builtin gmtime() function
  259. =item Time::localtime
  260. by-name interface to Perl's builtin localtime() function
  261. =item Time::tm
  262. internal object used by Time::gmtime and Time::localtime
  263. =item UNIVERSAL
  264. base class for ALL classes (blessed references)
  265. =item User::grent
  266. by-name interface to Perl's builtin getgr*() functions
  267. =item User::pwent
  268. by-name interface to Perl's builtin getpw*() functions
  269. =back
  270. To find out I<all> the modules installed on your system, including
  271. those without documentation or outside the standard release, do this:
  272. % find `perl -e 'print "@INC"'` -name '*.pm' -print
  273. They should all have their own documentation installed and accessible via
  274. your system man(1) command. If that fails, try the I<perldoc> program.
  275. =head2 Extension Modules
  276. Extension modules are written in C (or a mix of Perl and C) and may be
  277. statically linked or in general are
  278. dynamically loaded into Perl if and when you need them. Supported
  279. extension modules include the Socket, Fcntl, and POSIX modules.
  280. Many popular C extension modules do not come bundled (at least, not
  281. completely) due to their sizes, volatility, or simply lack of time for
  282. adequate testing and configuration across the multitude of platforms on
  283. which Perl was beta-tested. You are encouraged to look for them in
  284. archie(1L), the Perl FAQ or Meta-FAQ, the WWW page, and even with their
  285. authors before randomly posting asking for their present condition and
  286. disposition.
  287. =head1 CPAN
  288. CPAN stands for the Comprehensive Perl Archive Network. This is a globally
  289. replicated collection of all known Perl materials, including hundreds
  290. of unbundled modules. Here are the major categories of modules:
  291. =over
  292. =item *
  293. Language Extensions and Documentation Tools
  294. =item *
  295. Development Support
  296. =item *
  297. Operating System Interfaces
  298. =item *
  299. Networking, Device Control (modems) and InterProcess Communication
  300. =item *
  301. Data Types and Data Type Utilities
  302. =item *
  303. Database Interfaces
  304. =item *
  305. User Interfaces
  306. =item *
  307. Interfaces to / Emulations of Other Programming Languages
  308. =item *
  309. File Names, File Systems and File Locking (see also File Handles)
  310. =item *
  311. String Processing, Language Text Processing, Parsing, and Searching
  312. =item *
  313. Option, Argument, Parameter, and Configuration File Processing
  314. =item *
  315. Internationalization and Locale
  316. =item *
  317. Authentication, Security, and Encryption
  318. =item *
  319. World Wide Web, HTML, HTTP, CGI, MIME
  320. =item *
  321. Server and Daemon Utilities
  322. =item *
  323. Archiving and Compression
  324. =item *
  325. Images, Pixmap and Bitmap Manipulation, Drawing, and Graphing
  326. =item *
  327. Mail and Usenet News
  328. =item *
  329. Control Flow Utilities (callbacks and exceptions etc)
  330. =item *
  331. File Handle and Input/Output Stream Utilities
  332. =item *
  333. Miscellaneous Modules
  334. =back
  335. The registered CPAN sites as of this writing include the following.
  336. You should try to choose one close to you:
  337. =over
  338. =item *
  339. Africa
  340. South Africa ftp://ftp.is.co.za/programming/perl/CPAN/
  341. ftp://ftpza.co.za/pub/mirrors/cpan/
  342. =item *
  343. Asia
  344. Armenia ftp://sunsite.aua.am/pub/CPAN/
  345. China ftp://freesoft.cei.gov.cn/pub/languages/perl/CPAN/
  346. Hong Kong ftp://ftp.hkstar.com/pub/CPAN/
  347. Israel ftp://bioinfo.weizmann.ac.il/pub/software/perl/CPAN/
  348. Japan ftp://ftp.dti.ad.jp/pub/lang/CPAN/
  349. ftp://ftp.jaist.ac.jp/pub/lang/perl/CPAN/
  350. ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/
  351. ftp://ftp.meisei-u.ac.jp/pub/CPAN/
  352. ftp://mirror.nucba.ac.jp/mirror/Perl/
  353. Singapore ftp://ftp.nus.edu.sg/pub/unix/perl/CPAN/
  354. South Korea ftp://ftp.bora.net/pub/CPAN/
  355. ftp://ftp.nuri.net/pub/CPAN/
  356. Taiwan ftp://ftp.wownet.net/pub2/PERL/
  357. ftp://ftp1.sinica.edu.tw/pub1/perl/CPAN/
  358. Thailand ftp://ftp.cs.riubon.ac.th/pub/mirrors/CPAN/
  359. ftp://ftp.nectec.or.th/pub/mirrors/CPAN/
  360. =item *
  361. Australasia
  362. Australia ftp://cpan.topend.com.au/pub/CPAN/
  363. ftp://ftp.labyrinth.net.au/pub/perl/CPAN/
  364. ftp://ftp.sage-au.org.au/pub/compilers/perl/CPAN/
  365. ftp://mirror.aarnet.edu.au/pub/perl/CPAN/
  366. New Zealand ftp://ftp.auckland.ac.nz/pub/perl/CPAN/
  367. ftp://sunsite.net.nz/pub/languages/perl/CPAN/
  368. =item *
  369. Central America
  370. Costa Rica ftp://ftp.ucr.ac.cr/pub/Unix/CPAN/
  371. =item *
  372. Europe
  373. Austria ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/
  374. Belgium ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/
  375. Bulgaria ftp://ftp.ntrl.net/pub/mirrors/CPAN/
  376. Croatia ftp://ftp.linux.hr/pub/CPAN/
  377. Czech Republic ftp://ftp.fi.muni.cz/pub/perl/
  378. ftp://sunsite.mff.cuni.cz/Languages/Perl/CPAN/
  379. Denmark ftp://sunsite.auc.dk/pub/languages/perl/CPAN/
  380. Estonia ftp://ftp.ut.ee/pub/languages/perl/CPAN/
  381. Finland ftp://ftp.funet.fi/pub/languages/perl/CPAN/
  382. France ftp://ftp.lip6.fr/pub/perl/CPAN/
  383. ftp://ftp.oleane.net/pub/mirrors/CPAN/
  384. ftp://ftp.pasteur.fr/pub/computing/CPAN/
  385. Germany ftp://ftp.archive.de.uu.net/pub/CPAN/
  386. ftp://ftp.gmd.de/packages/CPAN/
  387. ftp://ftp.gwdg.de/pub/languages/perl/CPAN/
  388. ftp://ftp.leo.org/pub/comp/programming/languages/script/perl/CPAN/
  389. ftp://ftp.mpi-sb.mpg.de/pub/perl/CPAN/
  390. ftp://ftp.rz.ruhr-uni-bochum.de/pub/CPAN/
  391. ftp://ftp.uni-erlangen.de/pub/source/CPAN/
  392. ftp://ftp.uni-hamburg.de/pub/soft/lang/perl/CPAN/
  393. Greece ftp://ftp.ntua.gr/pub/lang/perl/
  394. Hungary ftp://ftp.kfki.hu/pub/packages/perl/CPAN/
  395. Ireland ftp://sunsite.compapp.dcu.ie/pub/perl/
  396. Italy ftp://cis.uniRoma2.it/CPAN/
  397. ftp://ftp.flashnet.it/pub/CPAN/
  398. ftp://ftp.unipi.it/pub/mirror/perl/CPAN/
  399. Netherlands ftp://ftp.cs.uu.nl/mirror/CPAN/
  400. ftp://ftp.nluug.nl/pub/languages/perl/CPAN/
  401. Norway ftp://ftp.uit.no/pub/languages/perl/cpan/
  402. ftp://sunsite.uio.no/pub/languages/perl/CPAN/
  403. Poland ftp://ftp.man.szczecin.pl/pub/perl/CPAN/
  404. ftp://ftp.man.torun.pl/pub/doc/CPAN/
  405. ftp://ftp.pk.edu.pl/pub/lang/perl/CPAN/
  406. ftp://sunsite.icm.edu.pl/pub/CPAN/
  407. Portugal ftp://ftp.ci.uminho.pt/pub/mirrors/cpan/
  408. ftp://ftp.ua.pt/pub/CPAN/
  409. Romania ftp://ftp.dntis.ro/pub/mirrors/perl-cpan/
  410. ftp://ftp.dnttm.ro/pub/CPAN/
  411. Russia ftp://cpan.npi.msu.su/CPAN/
  412. ftp://ftp.sai.msu.su/pub/lang/perl/CPAN/
  413. Slovakia ftp://ftp.entry.sk/pub/languages/perl/CPAN/
  414. Slovenia ftp://ftp.arnes.si/software/perl/CPAN/
  415. Spain ftp://ftp.etse.urv.es/pub/perl/
  416. ftp://ftp.rediris.es/mirror/CPAN/
  417. Sweden ftp://ftp.sunet.se/pub/lang/perl/CPAN/
  418. Switzerland ftp://sunsite.cnlab-switch.ch/mirror/CPAN/
  419. Turkey ftp://sunsite.bilkent.edu.tr/pub/languages/CPAN/
  420. United Kingdom ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/
  421. ftp://ftp.flirble.org/pub/languages/perl/CPAN/
  422. ftp://ftp.plig.org/pub/CPAN/
  423. ftp://sunsite.doc.ic.ac.uk/packages/CPAN/
  424. ftp://unix.hensa.ac.uk/mirrors/perl-CPAN/
  425. =item *
  426. North America
  427. Alberta ftp://sunsite.ualberta.ca/pub/Mirror/CPAN/
  428. California ftp://ftp.cdrom.com/pub/perl/CPAN/
  429. ftp://ftp.digital.com/pub/plan/perl/CPAN/
  430. Colorado ftp://ftp.cs.colorado.edu/pub/perl/CPAN/
  431. Florida ftp://ftp.cise.ufl.edu/pub/perl/CPAN/
  432. Illinois ftp://uiarchive.uiuc.edu/pub/lang/perl/CPAN/
  433. Indiana ftp://csociety-ftp.ecn.purdue.edu/pub/CPAN/
  434. ftp://ftp.uwsg.indiana.edu/pub/perl/CPAN/
  435. Manitoba ftp://theory.uwinnipeg.ca/pub/CPAN/
  436. Massachusetts ftp://ftp.ccs.neu.edu/net/mirrors/ftp.funet.fi/pub/languages/perl/CPAN/
  437. ftp://ftp.iguide.com/pub/mirrors/packages/perl/CPAN/
  438. Mexico D.F. ftp://ftp.msg.com.mx/pub/CPAN/
  439. New York ftp://ftp.rge.com/pub/languages/perl/
  440. North Carolina ftp://ftp.duke.edu/pub/perl/
  441. Oklahoma ftp://ftp.ou.edu/mirrors/CPAN/
  442. Ontario ftp://ftp.crc.ca/pub/packages/perl/CPAN/
  443. Oregon ftp://ftp.orst.edu/pub/packages/CPAN/
  444. Pennsylvania ftp://ftp.epix.net/pub/languages/perl/
  445. Texas ftp://ftp.sedl.org/pub/mirrors/CPAN/
  446. Utah ftp://mirror.xmission.com/CPAN/
  447. Virginia ftp://ftp.perl.org/pub/perl/CPAN/
  448. ftp://ruff.cs.jmu.edu/pub/CPAN/
  449. Washington ftp://ftp.spu.edu/pub/CPAN/
  450. =item *
  451. South America
  452. Brazil ftp://cpan.if.usp.br/pub/mirror/CPAN/
  453. Chile ftp://ftp.ing.puc.cl/pub/unix/perl/CPAN/
  454. ftp://sunsite.dcc.uchile.cl/pub/Lang/perl/CPAN/
  455. =back
  456. For an up-to-date listing of CPAN sites,
  457. see F<http://www.perl.com/perl/CPAN> or F<ftp://ftp.perl.com/perl/>.
  458. =head1 Modules: Creation, Use, and Abuse
  459. (The following section is borrowed directly from Tim Bunce's modules
  460. file, available at your nearest CPAN site.)
  461. Perl implements a class using a package, but the presence of a
  462. package doesn't imply the presence of a class. A package is just a
  463. namespace. A class is a package that provides subroutines that can be
  464. used as methods. A method is just a subroutine that expects, as its
  465. first argument, either the name of a package (for "static" methods),
  466. or a reference to something (for "virtual" methods).
  467. A module is a file that (by convention) provides a class of the same
  468. name (sans the .pm), plus an import method in that class that can be
  469. called to fetch exported symbols. This module may implement some of
  470. its methods by loading dynamic C or C++ objects, but that should be
  471. totally transparent to the user of the module. Likewise, the module
  472. might set up an AUTOLOAD function to slurp in subroutine definitions on
  473. demand, but this is also transparent. Only the F<.pm> file is required to
  474. exist. See L<perlsub>, L<perltoot>, and L<AutoLoader> for details about
  475. the AUTOLOAD mechanism.
  476. =head2 Guidelines for Module Creation
  477. =over 4
  478. =item Do similar modules already exist in some form?
  479. If so, please try to reuse the existing modules either in whole or
  480. by inheriting useful features into a new class. If this is not
  481. practical try to get together with the module authors to work on
  482. extending or enhancing the functionality of the existing modules.
  483. A perfect example is the plethora of packages in perl4 for dealing
  484. with command line options.
  485. If you are writing a module to expand an already existing set of
  486. modules, please coordinate with the author of the package. It
  487. helps if you follow the same naming scheme and module interaction
  488. scheme as the original author.
  489. =item Try to design the new module to be easy to extend and reuse.
  490. Use blessed references. Use the two argument form of bless to bless
  491. into the class name given as the first parameter of the constructor,
  492. e.g.,:
  493. sub new {
  494. my $class = shift;
  495. return bless {}, $class;
  496. }
  497. or even this if you'd like it to be used as either a static
  498. or a virtual method.
  499. sub new {
  500. my $self = shift;
  501. my $class = ref($self) || $self;
  502. return bless {}, $class;
  503. }
  504. Pass arrays as references so more parameters can be added later
  505. (it's also faster). Convert functions into methods where
  506. appropriate. Split large methods into smaller more flexible ones.
  507. Inherit methods from other modules if appropriate.
  508. Avoid class name tests like: C<die "Invalid" unless ref $ref eq 'FOO'>.
  509. Generally you can delete the "C<eq 'FOO'>" part with no harm at all.
  510. Let the objects look after themselves! Generally, avoid hard-wired
  511. class names as far as possible.
  512. Avoid C<$r-E<gt>Class::func()> where using C<@ISA=qw(... Class ...)> and
  513. C<$r-E<gt>func()> would work (see L<perlbot> for more details).
  514. Use autosplit so little used or newly added functions won't be a
  515. burden to programs that don't use them. Add test functions to
  516. the module after __END__ either using AutoSplit or by saying:
  517. eval join('',<main::DATA>) || die $@ unless caller();
  518. Does your module pass the 'empty subclass' test? If you say
  519. "C<@SUBCLASS::ISA = qw(YOURCLASS);>" your applications should be able
  520. to use SUBCLASS in exactly the same way as YOURCLASS. For example,
  521. does your application still work if you change: C<$obj = new YOURCLASS;>
  522. into: C<$obj = new SUBCLASS;> ?
  523. Avoid keeping any state information in your packages. It makes it
  524. difficult for multiple other packages to use yours. Keep state
  525. information in objects.
  526. Always use B<-w>. Try to C<use strict;> (or C<use strict qw(...);>).
  527. Remember that you can add C<no strict qw(...);> to individual blocks
  528. of code that need less strictness. Always use B<-w>. Always use B<-w>!
  529. Follow the guidelines in the perlstyle(1) manual.
  530. =item Some simple style guidelines
  531. The perlstyle manual supplied with Perl has many helpful points.
  532. Coding style is a matter of personal taste. Many people evolve their
  533. style over several years as they learn what helps them write and
  534. maintain good code. Here's one set of assorted suggestions that
  535. seem to be widely used by experienced developers:
  536. Use underscores to separate words. It is generally easier to read
  537. $var_names_like_this than $VarNamesLikeThis, especially for
  538. non-native speakers of English. It's also a simple rule that works
  539. consistently with VAR_NAMES_LIKE_THIS.
  540. Package/Module names are an exception to this rule. Perl informally
  541. reserves lowercase module names for 'pragma' modules like integer
  542. and strict. Other modules normally begin with a capital letter and
  543. use mixed case with no underscores (need to be short and portable).
  544. You may find it helpful to use letter case to indicate the scope
  545. or nature of a variable. For example:
  546. $ALL_CAPS_HERE constants only (beware clashes with Perl vars)
  547. $Some_Caps_Here package-wide global/static
  548. $no_caps_here function scope my() or local() variables
  549. Function and method names seem to work best as all lowercase.
  550. e.g., C<$obj-E<gt>as_string()>.
  551. You can use a leading underscore to indicate that a variable or
  552. function should not be used outside the package that defined it.
  553. =item Select what to export.
  554. Do NOT export method names!
  555. Do NOT export anything else by default without a good reason!
  556. Exports pollute the namespace of the module user. If you must
  557. export try to use @EXPORT_OK in preference to @EXPORT and avoid
  558. short or common names to reduce the risk of name clashes.
  559. Generally anything not exported is still accessible from outside the
  560. module using the ModuleName::item_name (or C<$blessed_ref-E<gt>method>)
  561. syntax. By convention you can use a leading underscore on names to
  562. indicate informally that they are 'internal' and not for public use.
  563. (It is actually possible to get private functions by saying:
  564. C<my $subref = sub { ... }; &$subref;>. But there's no way to call that
  565. directly as a method, because a method must have a name in the symbol
  566. table.)
  567. As a general rule, if the module is trying to be object oriented
  568. then export nothing. If it's just a collection of functions then
  569. @EXPORT_OK anything but use @EXPORT with caution.
  570. =item Select a name for the module.
  571. This name should be as descriptive, accurate, and complete as
  572. possible. Avoid any risk of ambiguity. Always try to use two or
  573. more whole words. Generally the name should reflect what is special
  574. about what the module does rather than how it does it. Please use
  575. nested module names to group informally or categorize a module.
  576. There should be a very good reason for a module not to have a nested name.
  577. Module names should begin with a capital letter.
  578. Having 57 modules all called Sort will not make life easy for anyone
  579. (though having 23 called Sort::Quick is only marginally better :-).
  580. Imagine someone trying to install your module alongside many others.
  581. If in any doubt ask for suggestions in comp.lang.perl.misc.
  582. If you are developing a suite of related modules/classes it's good
  583. practice to use nested classes with a common prefix as this will
  584. avoid namespace clashes. For example: Xyz::Control, Xyz::View,
  585. Xyz::Model etc. Use the modules in this list as a naming guide.
  586. If adding a new module to a set, follow the original author's
  587. standards for naming modules and the interface to methods in
  588. those modules.
  589. To be portable each component of a module name should be limited to
  590. 11 characters. If it might be used on MS-DOS then try to ensure each is
  591. unique in the first 8 characters. Nested modules make this easier.
  592. =item Have you got it right?
  593. How do you know that you've made the right decisions? Have you
  594. picked an interface design that will cause problems later? Have
  595. you picked the most appropriate name? Do you have any questions?
  596. The best way to know for sure, and pick up many helpful suggestions,
  597. is to ask someone who knows. Comp.lang.perl.misc is read by just about
  598. all the people who develop modules and it's the best place to ask.
  599. All you need to do is post a short summary of the module, its
  600. purpose and interfaces. A few lines on each of the main methods is
  601. probably enough. (If you post the whole module it might be ignored
  602. by busy people - generally the very people you want to read it!)
  603. Don't worry about posting if you can't say when the module will be
  604. ready - just say so in the message. It might be worth inviting
  605. others to help you, they may be able to complete it for you!
  606. =item README and other Additional Files.
  607. It's well known that software developers usually fully document the
  608. software they write. If, however, the world is in urgent need of
  609. your software and there is not enough time to write the full
  610. documentation please at least provide a README file containing:
  611. =over 10
  612. =item *
  613. A description of the module/package/extension etc.
  614. =item *
  615. A copyright notice - see below.
  616. =item *
  617. Prerequisites - what else you may need to have.
  618. =item *
  619. How to build it - possible changes to Makefile.PL etc.
  620. =item *
  621. How to install it.
  622. =item *
  623. Recent changes in this release, especially incompatibilities
  624. =item *
  625. Changes / enhancements you plan to make in the future.
  626. =back
  627. If the README file seems to be getting too large you may wish to
  628. split out some of the sections into separate files: INSTALL,
  629. Copying, ToDo etc.
  630. =over 4
  631. =item Adding a Copyright Notice.
  632. How you choose to license your work is a personal decision.
  633. The general mechanism is to assert your Copyright and then make
  634. a declaration of how others may copy/use/modify your work.
  635. Perl, for example, is supplied with two types of licence: The GNU
  636. GPL and The Artistic Licence (see the files README, Copying, and
  637. Artistic). Larry has good reasons for NOT just using the GNU GPL.
  638. My personal recommendation, out of respect for Larry, Perl, and the
  639. Perl community at large is to state something simply like:
  640. Copyright (c) 1995 Your Name. All rights reserved.
  641. This program is free software; you can redistribute it and/or
  642. modify it under the same terms as Perl itself.
  643. This statement should at least appear in the README file. You may
  644. also wish to include it in a Copying file and your source files.
  645. Remember to include the other words in addition to the Copyright.
  646. =item Give the module a version/issue/release number.
  647. To be fully compatible with the Exporter and MakeMaker modules you
  648. should store your module's version number in a non-my package
  649. variable called $VERSION. This should be a floating point
  650. number with at least two digits after the decimal (i.e., hundredths,
  651. e.g, C<$VERSION = "0.01">). Don't use a "1.3.2" style version.
  652. See Exporter.pm in Perl5.001m or later for details.
  653. It may be handy to add a function or method to retrieve the number.
  654. Use the number in announcements and archive file names when
  655. releasing the module (ModuleName-1.02.tar.Z).
  656. See perldoc ExtUtils::MakeMaker.pm for details.
  657. =item How to release and distribute a module.
  658. It's good idea to post an announcement of the availability of your
  659. module (or the module itself if small) to the comp.lang.perl.announce
  660. Usenet newsgroup. This will at least ensure very wide once-off
  661. distribution.
  662. If possible you should place the module into a major ftp archive and
  663. include details of its location in your announcement.
  664. Some notes about ftp archives: Please use a long descriptive file
  665. name that includes the version number. Most incoming directories
  666. will not be readable/listable, i.e., you won't be able to see your
  667. file after uploading it. Remember to send your email notification
  668. message as soon as possible after uploading else your file may get
  669. deleted automatically. Allow time for the file to be processed
  670. and/or check the file has been processed before announcing its
  671. location.
  672. FTP Archives for Perl Modules:
  673. Follow the instructions and links on
  674. http://franz.ww.tu-berlin.de/modulelist
  675. or upload to one of these sites:
  676. ftp://franz.ww.tu-berlin.de/incoming
  677. ftp://ftp.cis.ufl.edu/incoming
  678. and notify <F<[email protected]>>.
  679. By using the WWW interface you can ask the Upload Server to mirror
  680. your modules from your ftp or WWW site into your own directory on
  681. CPAN!
  682. Please remember to send me an updated entry for the Module list!
  683. =item Take care when changing a released module.
  684. Always strive to remain compatible with previous released versions.
  685. Otherwise try to add a mechanism to revert to the
  686. old behaviour if people rely on it. Document incompatible changes.
  687. =back
  688. =back
  689. =head2 Guidelines for Converting Perl 4 Library Scripts into Modules
  690. =over 4
  691. =item There is no requirement to convert anything.
  692. If it ain't broke, don't fix it! Perl 4 library scripts should
  693. continue to work with no problems. You may need to make some minor
  694. changes (like escaping non-array @'s in double quoted strings) but
  695. there is no need to convert a .pl file into a Module for just that.
  696. =item Consider the implications.
  697. All Perl applications that make use of the script will need to
  698. be changed (slightly) if the script is converted into a module. Is
  699. it worth it unless you plan to make other changes at the same time?
  700. =item Make the most of the opportunity.
  701. If you are going to convert the script to a module you can use the
  702. opportunity to redesign the interface. The 'Guidelines for Module
  703. Creation' above include many of the issues you should consider.
  704. =item The pl2pm utility will get you started.
  705. This utility will read *.pl files (given as parameters) and write
  706. corresponding *.pm files. The pl2pm utilities does the following:
  707. =over 10
  708. =item *
  709. Adds the standard Module prologue lines
  710. =item *
  711. Converts package specifiers from ' to ::
  712. =item *
  713. Converts die(...) to croak(...)
  714. =item *
  715. Several other minor changes
  716. =back
  717. Being a mechanical process pl2pm is not bullet proof. The converted
  718. code will need careful checking, especially any package statements.
  719. Don't delete the original .pl file till the new .pm one works!
  720. =back
  721. =head2 Guidelines for Reusing Application Code
  722. =over 4
  723. =item Complete applications rarely belong in the Perl Module Library.
  724. =item Many applications contain some Perl code that could be reused.
  725. Help save the world! Share your code in a form that makes it easy
  726. to reuse.
  727. =item Break-out the reusable code into one or more separate module files.
  728. =item Take the opportunity to reconsider and redesign the interfaces.
  729. =item In some cases the 'application' can then be reduced to a small
  730. fragment of code built on top of the reusable modules. In these cases
  731. the application could invoked as:
  732. % perl -e 'use Module::Name; method(@ARGV)' ...
  733. or
  734. % perl -mModule::Name ... (in perl5.002 or higher)
  735. =back
  736. =head1 NOTE
  737. Perl does not enforce private and public parts of its modules as you may
  738. have been used to in other languages like C++, Ada, or Modula-17. Perl
  739. doesn't have an infatuation with enforced privacy. It would prefer
  740. that you stayed out of its living room because you weren't invited, not
  741. because it has a shotgun.
  742. The module and its user have a contract, part of which is common law,
  743. and part of which is "written". Part of the common law contract is
  744. that a module doesn't pollute any namespace it wasn't asked to. The
  745. written contract for the module (A.K.A. documentation) may make other
  746. provisions. But then you know when you C<use RedefineTheWorld> that
  747. you're redefining the world and willing to take the consequences.