/usr/share/doc/perl-doc-html/perlmodstyle.html is in perl-doc-html 5.22.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>perlmodstyle - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-gb">
<link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
<link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
<link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
<div id="page">
<div id="header">
<div id="homepage_link">
<a href="index.html"></a>
</div>
<div id="strapline">
Perl Programming Documentation
</div>
<div id="download_link" class="download">
<a href="http://www.perl.org/get.html">Download Perl</a>
</div>
<div id="explore_link" class="download">
<a id="explore_anchor" href="#">Explore</a>
</div>
</div>
<div id="body">
<div id="left_column">
<div class="side_group">
<div class="side_panel doc_panel">
<p>Manual</p>
<ul>
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">History / Changes</a>
<li><a href="index-licence.html">License</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Reference</p>
<ul>
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Special Variables</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platform Specific</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Modules</p>
<ul>
<li>
<a href="index-modules-A.html">A</a>
•
<a href="index-modules-B.html">B</a>
•
<a href="index-modules-C.html">C</a>
•
<a href="index-modules-D.html">D</a>
•
<a href="index-modules-E.html">E</a>
<li>
<a href="index-modules-F.html">F</a>
•
<a href="index-modules-G.html">G</a>
•
<a href="index-modules-H.html">H</a>
•
<a href="index-modules-I.html">I</a>
•
<a href="index-modules-L.html">L</a>
<li>
<a href="index-modules-M.html">M</a>
•
<a href="index-modules-N.html">N</a>
•
<a href="index-modules-O.html">O</a>
•
<a href="index-modules-P.html">P</a>
•
<a href="index-modules-S.html">S</a>
<li>
<a href="index-modules-T.html">T</a>
•
<a href="index-modules-U.html">U</a>
•
<a href="index-modules-X.html">X</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Tools</p>
<ul>
<li><a href="preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div id="centre_column">
<div id="content_header">
<div id="title_bar">
<div id="page_name">
<h1>perlmodstyle</h1>
</div>
<div id="perl_version">
Perl 5 version 22.0 documentation
</div>
<div class="page_links" id="page_links_top">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
</div>
<div class="page_links" id="page_links_bottom">
<a href="#" id="page_index_toggle">Show page index</a> •
<a href="#" id="recent_pages_toggle">Show recent pages</a>
</div>
<div id="search_form">
<form action="search.html" method="GET" id="search">
<input type="text" name="q" id="search_box" alt="Search">
</form>
</div>
</div>
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-language.html">Language reference</a> >
perlmodstyle
</div>
</div>
<div id="content_body">
<!--[if lt IE 7]>
<div class="noscript">
<p>
<strong>It looks like you're using Internet Explorer 6. This is a very old
browser which does not offer full support for modern websites.</strong>
</p>
<p>
Unfortunately this means that this website will not work on
your computer.
</p>
<p>
Don't miss out though! To view the site (and get a better experience from
many other websites), simply upgrade to
<a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
or download an alternative browser such as
<a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
<a href="http://www.apple.com/safari/download/">Safari</a>, or
<a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
<p>
All of these browsers are free. If you're using a PC at work, you may
need to contact your IT administrator.
</p>
</div>
<![endif]-->
<noscript>
<div class="noscript">
<p>
<strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
or are running a non-JavaScript capable web browser.</strong>
</p>
<p>
To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
</div>
</noscript>
<div id="recent_pages" class="hud_container">
<div id="recent_pages_header" class="hud_header">
<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
<div id="recent_pages_topright" class="hud_topright"></div>
</div>
<div id="recent_pages_content" class="hud_content">
</div>
<div id="recent_pages_footer" class="hud_footer">
<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="recent_pages_resize" class="hud_resize"></div>
</div>
</div>
<div id="from_search"></div>
<h1>perlmodstyle</h1>
<!-- -->
<ul><li><a href="#NAME">NAME</a><li><a href="#INTRODUCTION">INTRODUCTION</a><li><a href="#QUICK-CHECKLIST">QUICK CHECKLIST</a><ul><li><a href="#Before-you-start">Before you start</a><li><a href="#The-API">The API</a><li><a href="#Stability">Stability</a><li><a href="#Documentation">Documentation</a><li><a href="#Release-considerations">Release considerations</a></ul><li><a href="#BEFORE-YOU-START-WRITING-A-MODULE">BEFORE YOU START WRITING A MODULE</a><ul><li><a href="#Has-it-been-done-before%3f">Has it been done before?</a><li><a href="#Do-one-thing-and-do-it-well">Do one thing and do it well</a><li><a href="#What's-in-a-name%3f">What's in a name?</a><li><a href="#Get-feedback-before-publishing">Get feedback before publishing</a></ul><li><a href="#DESIGNING-AND-WRITING-YOUR-MODULE">DESIGNING AND WRITING YOUR MODULE</a><ul><li><a href="#To-OO-or-not-to-OO%3f">To OO or not to OO?</a><li><a href="#Designing-your-API">Designing your API</a><li><a href="#Strictness-and-warnings">Strictness and warnings</a><li><a href="#Backwards-compatibility">Backwards compatibility</a><li><a href="#Error-handling-and-messages">Error handling and messages</a></ul><li><a href="#DOCUMENTING-YOUR-MODULE">DOCUMENTING YOUR MODULE</a><ul><li><a href="#POD">POD</a><li><a href="#README%2c-INSTALL%2c-release-notes%2c-changelogs">README, INSTALL, release notes, changelogs</a></ul><li><a href="#RELEASE-CONSIDERATIONS">RELEASE CONSIDERATIONS</a><ul><li><a href="#Version-numbering">Version numbering</a><li><a href="#Pre-requisites">Pre-requisites</a><li><a href="#Testing">Testing</a><li><a href="#Packaging">Packaging</a><li><a href="#Licensing">Licensing</a></ul><li><a href="#COMMON-PITFALLS">COMMON PITFALLS</a><ul><li><a href="#Reinventing-the-wheel">Reinventing the wheel</a><li><a href="#Trying-to-do-too-much">Trying to do too much</a><li><a href="#Inappropriate-documentation">Inappropriate documentation</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlmodstyle - Perl module style guide</p>
<a name="INTRODUCTION"></a><h1>INTRODUCTION</h1>
<p>This document attempts to describe the Perl Community's "best practice"
for writing Perl modules. It extends the recommendations found in
<a href="perlstyle.html">perlstyle</a> , which should be considered required reading
before reading this document.</p>
<p>While this document is intended to be useful to all module authors, it is
particularly aimed at authors who wish to publish their modules on CPAN.</p>
<p>The focus is on elements of style which are visible to the users of a
module, rather than those parts which are only seen by the module's
developers. However, many of the guidelines presented in this document
can be extrapolated and applied successfully to a module's internals.</p>
<p>This document differs from <a href="perlnewmod.html">perlnewmod</a> in that it is a style guide
rather than a tutorial on creating CPAN modules. It provides a
checklist against which modules can be compared to determine whether
they conform to best practice, without necessarily describing in detail
how to achieve this.</p>
<p>All the advice contained in this document has been gleaned from
extensive conversations with experienced CPAN authors and users. Every
piece of advice given here is the result of previous mistakes. This
information is here to help you avoid the same mistakes and the extra
work that would inevitably be required to fix them.</p>
<p>The first section of this document provides an itemized checklist;
subsequent sections provide a more detailed discussion of the items on
the list. The final section, "Common Pitfalls", describes some of the
most popular mistakes made by CPAN authors.</p>
<a name="QUICK-CHECKLIST"></a><h1>QUICK CHECKLIST</h1>
<p>For more detail on each item in this checklist, see below.</p>
<a name="Before-you-start"></a><h2>Before you start</h2>
<ul>
<li>
<p>Don't re-invent the wheel</p>
</li>
<li>
<p>Patch, extend or subclass an existing module where possible</p>
</li>
<li>
<p>Do one thing and do it well</p>
</li>
<li>
<p>Choose an appropriate name</p>
</li>
<li>
<p>Get feedback before publishing</p>
</li>
</ul>
<a name="The-API"></a><h2>The API</h2>
<ul>
<li>
<p>API should be understandable by the average programmer</p>
</li>
<li>
<p>Simple methods for simple tasks</p>
</li>
<li>
<p>Separate functionality from output</p>
</li>
<li>
<p>Consistent naming of subroutines or methods</p>
</li>
<li>
<p>Use named parameters (a hash or hashref) when there are more than two
parameters</p>
</li>
</ul>
<a name="Stability"></a><h2>Stability</h2>
<ul>
<li>
<p>Ensure your module works under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
and <code class="inline">-w</code>
</p>
</li>
<li>
<p>Stable modules should maintain backwards compatibility</p>
</li>
</ul>
<a name="Documentation"></a><h2>Documentation</h2>
<ul>
<li>
<p>Write documentation in POD</p>
</li>
<li>
<p>Document purpose, scope and target applications</p>
</li>
<li>
<p>Document each publically accessible method or subroutine, including params and return values</p>
</li>
<li>
<p>Give examples of use in your documentation</p>
</li>
<li>
<p>Provide a README file and perhaps also release notes, changelog, etc</p>
</li>
<li>
<p>Provide links to further information (URL, email)</p>
</li>
</ul>
<a name="Release-considerations"></a><h2>Release considerations</h2>
<ul>
<li>
<p>Specify pre-requisites in Makefile.PL or Build.PL</p>
</li>
<li>
<p>Specify Perl version requirements with <code class="inline"><a class="l_k" href="functions/use.html">use</a></code></p>
</li>
<li>
<p>Include tests with your module</p>
</li>
<li>
<p>Choose a sensible and consistent version numbering scheme (X.YY is the common Perl module numbering scheme)</p>
</li>
<li>
<p>Increment the version number for every change, no matter how small</p>
</li>
<li>
<p>Package the module using "make dist"</p>
</li>
<li>
<p>Choose an appropriate license (GPL/Artistic is a good default)</p>
</li>
</ul>
<a name="BEFORE-YOU-START-WRITING-A-MODULE"></a><h1>BEFORE YOU START WRITING A MODULE</h1>
<p>Try not to launch headlong into developing your module without spending
some time thinking first. A little forethought may save you a vast
amount of effort later on.</p>
<a name="Has-it-been-done-before%3f"></a><h2>Has it been done before?</h2>
<p>You may not even need to write the module. Check whether it's already
been done in Perl, and avoid re-inventing the wheel unless you have a
good reason.</p>
<p>Good places to look for pre-existing modules include
<a href="http://search.cpan.org/">http://search.cpan.org/</a> and <a href="https://metacpan.org">https://metacpan.org</a>
and asking on <code class="inline"><span class="w">module</span>-<span class="w">authors</span><span class="i">@perl</span>.<span class="w">org</span></code>
(<a href="http://lists.perl.org/list/module-authors.html">http://lists.perl.org/list/module-authors.html</a>).</p>
<p>If an existing module <b>almost</b> does what you want, consider writing a
patch, writing a subclass, or otherwise extending the existing module
rather than rewriting it.</p>
<a name="Do-one-thing-and-do-it-well"></a><h2>Do one thing and do it well</h2>
<p>At the risk of stating the obvious, modules are intended to be modular.
A Perl developer should be able to use modules to put together the
building blocks of their application. However, it's important that the
blocks are the right shape, and that the developer shouldn't have to use
a big block when all they need is a small one.</p>
<p>Your module should have a clearly defined scope which is no longer than
a single sentence. Can your module be broken down into a family of
related modules?</p>
<p>Bad example:</p>
<p>"FooBar.pm provides an implementation of the FOO protocol and the
related BAR standard."</p>
<p>Good example:</p>
<p>"Foo.pm provides an implementation of the FOO protocol. Bar.pm
implements the related BAR protocol."</p>
<p>This means that if a developer only needs a module for the BAR standard,
they should not be forced to install libraries for FOO as well.</p>
<a name="What's-in-a-name%3f"></a><h2>What's in a name?</h2>
<p>Make sure you choose an appropriate name for your module early on. This
will help people find and remember your module, and make programming
with your module more intuitive.</p>
<p>When naming your module, consider the following:</p>
<ul>
<li>
<p>Be descriptive (i.e. accurately describes the purpose of the module).</p>
</li>
<li>
<p>Be consistent with existing modules.</p>
</li>
<li>
<p>Reflect the functionality of the module, not the implementation.</p>
</li>
<li>
<p>Avoid starting a new top-level hierarchy, especially if a suitable
hierarchy already exists under which you could place your module.</p>
</li>
</ul>
<a name="Get-feedback-before-publishing"></a><h2>Get feedback before publishing</h2>
<p>If you have never uploaded a module to CPAN before (and even if you have),
you are strongly encouraged to get feedback on <a href="http://search.cpan.org/perldoc/http:#%2fprepan.org">PrePAN</a>.
PrePAN is a site dedicated to discussing ideas for CPAN modules with other
Perl developers and is a great resource for new (and experienced) Perl
developers.</p>
<p>You should also try to get feedback from people who are already familiar
with the module's application domain and the CPAN naming system. Authors
of similar modules, or modules with similar names, may be a good place to
start, as are community sites like <a href="http://search.cpan.org/perldoc/http:#%2fwww.perlmonks.org">Perl Monks</a>.</p>
<a name="DESIGNING-AND-WRITING-YOUR-MODULE"></a><h1>DESIGNING AND WRITING YOUR MODULE</h1>
<p>Considerations for module design and coding:</p>
<a name="To-OO-or-not-to-OO%3f"></a><h2>To OO or not to OO?</h2>
<p>Your module may be object oriented (OO) or not, or it may have both kinds
of interfaces available. There are pros and cons of each technique, which
should be considered when you design your API.</p>
<p>In <i>Perl Best Practices</i> (copyright 2004, Published by O'Reilly Media, Inc.),
Damian Conway provides a list of criteria to use when deciding if OO is the
right fit for your problem:</p>
<ul>
<li>
<p>The system being designed is large, or is likely to become large.</p>
</li>
<li>
<p>The data can be aggregated into obvious structures, especially if
there's a large amount of data in each aggregate.</p>
</li>
<li>
<p>The various types of data aggregate form a natural hierarchy that
facilitates the use of inheritance and polymorphism.</p>
</li>
<li>
<p>You have a piece of data on which many different operations are
applied.</p>
</li>
<li>
<p>You need to perform the same general operations on related types of
data, but with slight variations depending on the specific type of data
the operations are applied to.</p>
</li>
<li>
<p>It's likely you'll have to add new data types later.</p>
</li>
<li>
<p>The typical interactions between pieces of data are best represented by
operators.</p>
</li>
<li>
<p>The implementation of individual components of the system is likely to
change over time.</p>
</li>
<li>
<p>The system design is already object-oriented.</p>
</li>
<li>
<p>Large numbers of other programmers will be using your code modules.</p>
</li>
</ul>
<p>Think carefully about whether OO is appropriate for your module.
Gratuitous object orientation results in complex APIs which are
difficult for the average module user to understand or use.</p>
<a name="Designing-your-API"></a><h2>Designing your API</h2>
<p>Your interfaces should be understandable by an average Perl programmer.
The following guidelines may help you judge whether your API is
sufficiently straightforward:</p>
<ul>
<li><a name="Write-simple-routines-to-do-simple-things."></a><b>Write simple routines to do simple things.</b>
<p>It's better to have numerous simple routines than a few monolithic ones.
If your routine changes its behaviour significantly based on its
arguments, it's a sign that you should have two (or more) separate
routines.</p>
</li>
<li><a name="Separate-functionality-from-output."></a><b>Separate functionality from output.</b>
<p>Return your results in the most generic form possible and allow the user
to choose how to use them. The most generic form possible is usually a
Perl data structure which can then be used to generate a text report,
HTML, XML, a database query, or whatever else your users require.</p>
<p>If your routine iterates through some kind of list (such as a list of
files, or records in a database) you may consider providing a callback
so that users can manipulate each element of the list in turn.
File::Find provides an example of this with its
<code class="inline"><span class="i">find</span><span class="s">(</span>\<span class="i">&wanted</span><span class="cm">,</span> <span class="i">$dir</span><span class="s">)</span></code>
syntax.</p>
</li>
<li><a name="Provide-sensible-shortcuts-and-defaults."></a><b>Provide sensible shortcuts and defaults.</b>
<p>Don't require every module user to jump through the same hoops to achieve a
simple result. You can always include optional parameters or routines for
more complex or non-standard behaviour. If most of your users have to
type a few almost identical lines of code when they start using your
module, it's a sign that you should have made that behaviour a default.
Another good indicator that you should use defaults is if most of your
users call your routines with the same arguments.</p>
</li>
<li><a name="Naming-conventions"></a><b>Naming conventions</b>
<p>Your naming should be consistent. For instance, it's better to have:</p>
<pre class="verbatim"><ol><li> <span class="i">display_day</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">display_week</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">display_year</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>than</p>
<pre class="verbatim"><ol><li> <span class="i">display_day</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">week_display</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">show_year</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This applies equally to method names, parameter names, and anything else
which is visible to the user (and most things that aren't!)</p>
</li>
<li><a name="Parameter-passing"></a><b>Parameter passing</b>
<p>Use named parameters. It's easier to use a hash like this:</p>
<pre class="verbatim"><ol><li> <span class="i">$obj</span><span class="i">->do_something</span><span class="s">(</span></li><li> <span class="w">name</span> <span class="cm">=></span> <span class="q">"wibble"</span><span class="cm">,</span></li><li> <span class="w">type</span> <span class="cm">=></span> <span class="q">"text"</span><span class="cm">,</span></li><li> <span class="w">size</span> <span class="cm">=></span> <span class="n">1024</span><span class="cm">,</span></li><li> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>... than to have a long list of unnamed parameters like this:</p>
<pre class="verbatim"><ol><li> <span class="i">$obj</span><span class="i">->do_something</span><span class="s">(</span><span class="q">"wibble"</span><span class="cm">,</span> <span class="q">"text"</span><span class="cm">,</span> <span class="n">1024</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>While the list of arguments might work fine for one, two or even three
arguments, any more arguments become hard for the module user to
remember, and hard for the module author to manage. If you want to add
a new parameter you will have to add it to the end of the list for
backward compatibility, and this will probably make your list order
unintuitive. Also, if many elements may be undefined you may see the
following unattractive method calls:</p>
<pre class="verbatim"><ol><li> <span class="i">$obj</span><span class="i">->do_something</span><span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="n">1024</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Provide sensible defaults for parameters which have them. Don't make
your users specify parameters which will almost always be the same.</p>
<p>The issue of whether to pass the arguments in a hash or a hashref is
largely a matter of personal style.</p>
<p>The use of hash keys starting with a hyphen (<code class="inline">-<span class="w">name</span></code>
) or entirely in
upper case (<code class="inline"><span class="w">NAME</span></code>
) is a relic of older versions of Perl in which
ordinary lower case strings were not handled correctly by the <code class="inline"><span class="cm">=></span></code>
operator. While some modules retain uppercase or hyphenated argument
keys for historical reasons or as a matter of personal style, most new
modules should use simple lower case keys. Whatever you choose, be
consistent!</p>
</li>
</ul>
<a name="Strictness-and-warnings"></a><h2>Strictness and warnings</h2>
<p>Your module should run successfully under the strict pragma and should
run without generating any warnings. Your module should also handle
taint-checking where appropriate, though this can cause difficulties in
many cases.</p>
<a name="Backwards-compatibility"></a><h2>Backwards compatibility</h2>
<p>Modules which are "stable" should not break backwards compatibility
without at least a long transition phase and a major change in version
number.</p>
<a name="Error-handling-and-messages"></a><h2>Error handling and messages</h2>
<p>When your module encounters an error it should do one or more of:</p>
<ul>
<li>
<p>Return an undefined value.</p>
</li>
<li>
<p>set <code class="inline"><span class="i">$Module::errstr</span></code>
or similar (<code class="inline"><span class="w">errstr</span></code>
is a common name used by
DBI and other popular modules; if you choose something else, be sure to
document it clearly).</p>
</li>
<li>
<p><code class="inline"><a class="l_k" href="functions/warn.html">warn()</a></code> or <code class="inline"><span class="i">carp</span><span class="s">(</span><span class="s">)</span></code>
a message to STDERR.</p>
</li>
<li>
<p><code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
only when your module absolutely cannot figure out what to
do. (<code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
is a better version of <code class="inline"><a class="l_k" href="functions/die.html">die()</a></code> for use within
modules, which reports its errors from the perspective of the caller.
See <a href="Carp.html">Carp</a> for details of <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><span class="i">carp</span><span class="s">(</span><span class="s">)</span></code>
and other useful
routines.)</p>
</li>
<li>
<p>As an alternative to the above, you may prefer to throw exceptions using
the Error module.</p>
</li>
</ul>
<p>Configurable error handling can be very useful to your users. Consider
offering a choice of levels for warning and debug messages, an option to
send messages to a separate file, a way to specify an error-handling
routine, or other such features. Be sure to default all these options
to the commonest use.</p>
<a name="DOCUMENTING-YOUR-MODULE"></a><h1>DOCUMENTING YOUR MODULE</h1>
<a name="POD"></a><h2>POD</h2>
<p>Your module should include documentation aimed at Perl developers.
You should use Perl's "plain old documentation" (POD) for your general
technical documentation, though you may wish to write additional
documentation (white papers, tutorials, etc) in some other format.
You need to cover the following subjects:</p>
<ul>
<li>
<p>A synopsis of the common uses of the module</p>
</li>
<li>
<p>The purpose, scope and target applications of your module</p>
</li>
<li>
<p>Use of each publically accessible method or subroutine, including
parameters and return values</p>
</li>
<li>
<p>Examples of use</p>
</li>
<li>
<p>Sources of further information</p>
</li>
<li>
<p>A contact email address for the author/maintainer</p>
</li>
</ul>
<p>The level of detail in Perl module documentation generally goes from
less detailed to more detailed. Your SYNOPSIS section should contain a
minimal example of use (perhaps as little as one line of code; skip the
unusual use cases or anything not needed by most users); the
DESCRIPTION should describe your module in broad terms, generally in
just a few paragraphs; more detail of the module's routines or methods,
lengthy code examples, or other in-depth material should be given in
subsequent sections.</p>
<p>Ideally, someone who's slightly familiar with your module should be able
to refresh their memory without hitting "page down". As your reader
continues through the document, they should receive a progressively
greater amount of knowledge.</p>
<p>The recommended order of sections in Perl module documentation is:</p>
<ul>
<li>
<p>NAME</p>
</li>
<li>
<p>SYNOPSIS</p>
</li>
<li>
<p>DESCRIPTION</p>
</li>
<li>
<p>One or more sections or subsections giving greater detail of available
methods and routines and any other relevant information.</p>
</li>
<li>
<p>BUGS/CAVEATS/etc</p>
</li>
<li>
<p>AUTHOR</p>
</li>
<li>
<p>SEE ALSO</p>
</li>
<li>
<p>COPYRIGHT and LICENSE</p>
</li>
</ul>
<p>Keep your documentation near the code it documents ("inline"
documentation). Include POD for a given method right above that
method's subroutine. This makes it easier to keep the documentation up
to date, and avoids having to document each piece of code twice (once in
POD and once in comments).</p>
<a name="README%2c-INSTALL%2c-release-notes%2c-changelogs"></a><h2>README, INSTALL, release notes, changelogs</h2>
<p>Your module should also include a README file describing the module and
giving pointers to further information (website, author email).</p>
<p>An INSTALL file should be included, and should contain simple installation
instructions. When using ExtUtils::MakeMaker this will usually be:</p>
<ul>
<li><a name="perl-Makefile.PL"></a><b>perl Makefile.PL</b>
</li>
<li><a name="make"></a><b>make</b>
</li>
<li><a name="make-test"></a><b>make test</b>
</li>
<li><a name="make-install"></a><b>make install</b>
</li>
</ul>
<p>When using Module::Build, this will usually be:</p>
<ul>
<li><a name="perl-Build.PL"></a><b>perl Build.PL</b>
</li>
<li><a name="perl-Build"></a><b>perl Build</b>
</li>
<li><a name="perl-Build-test"></a><b>perl Build test</b>
</li>
<li><a name="perl-Build-install"></a><b>perl Build install</b>
</li>
</ul>
<p>Release notes or changelogs should be produced for each release of your
software describing user-visible changes to your module, in terms
relevant to the user.</p>
<p>Unless you have good reasons for using some other format
(for example, a format used within your company),
the convention is to name your changelog file <code class="inline"><span class="w">Changes</span></code>
,
and to follow the simple format described in <a href="http://search.cpan.org/perldoc/CPAN::Changes::Spec">CPAN::Changes::Spec</a>.</p>
<a name="RELEASE-CONSIDERATIONS"></a><h1>RELEASE CONSIDERATIONS</h1>
<a name="Version-numbering"></a><h2>Version numbering</h2>
<p>Version numbers should indicate at least major and minor releases, and
possibly sub-minor releases. A major release is one in which most of
the functionality has changed, or in which major new functionality is
added. A minor release is one in which a small amount of functionality
has been added or changed. Sub-minor version numbers are usually used
for changes which do not affect functionality, such as documentation
patches.</p>
<p>The most common CPAN version numbering scheme looks like this:</p>
<pre class="verbatim"><ol><li> <span class="n">1.00</span><span class="cm">,</span> <span class="n">1.10</span><span class="cm">,</span> <span class="n">1.11</span><span class="cm">,</span> <span class="n">1.20</span><span class="cm">,</span> <span class="n">1.30</span><span class="cm">,</span> <span class="n">1.31</span><span class="cm">,</span> <span class="n">1.32</span></li></ol></pre><p>A correct CPAN version number is a floating point number with at least
2 digits after the decimal. You can test whether it conforms to CPAN by
using</p>
<pre class="verbatim"><ol><li> perl -MExtUtils::MakeMaker -le 'print MM->parse_version(shift)' 'Foo.pm'</li></ol></pre><p>If you want to release a 'beta' or 'alpha' version of a module but
don't want CPAN.pm to list it as most recent use an '_' after the
regular version number followed by at least 2 digits, eg. 1.20_01. If
you do this, the following idiom is recommended:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/our.html">our</a> <span class="i">$VERSION</span> = <span class="q">"1.12_01"</span><span class="sc">;</span> <span class="c"># so CPAN distribution will have</span></li><li> <span class="c"># right filename</span></li><li> <a class="l_k" href="functions/our.html">our</a> <span class="i">$XS_VERSION</span> = <span class="i">$VERSION</span><span class="sc">;</span> <span class="c"># only needed if you have XS code</span></li><li> <span class="i">$VERSION</span> = <a class="l_k" href="functions/eval.html">eval</a> <span class="i">$VERSION</span><span class="sc">;</span> <span class="c"># so "use Module 0.002" won't warn on</span></li><li> <span class="c"># underscore</span></li></ol></pre><p>With that trick MakeMaker will only read the first line and thus read
the underscore, while the perl interpreter will evaluate the $VERSION
and convert the string into a number. Later operations that treat
$VERSION as a number will then be able to do so without provoking a
warning about $VERSION not being a number.</p>
<p>Never release anything (even a one-word documentation patch) without
incrementing the number. Even a one-word documentation patch should
result in a change in version at the sub-minor level.</p>
<p>Once picked, it is important to stick to your version scheme, without
reducing the number of digits. This is because "downstream" packagers,
such as the FreeBSD ports system, interpret the version numbers in
various ways. If you change the number of digits in your version scheme,
you can confuse these systems so they get the versions of your module
out of order, which is obviously bad.</p>
<a name="Pre-requisites"></a><h2>Pre-requisites</h2>
<p>Module authors should carefully consider whether to rely on other
modules, and which modules to rely on.</p>
<p>Most importantly, choose modules which are as stable as possible. In
order of preference:</p>
<ul>
<li>
<p>Core Perl modules</p>
</li>
<li>
<p>Stable CPAN modules</p>
</li>
<li>
<p>Unstable CPAN modules</p>
</li>
<li>
<p>Modules not available from CPAN</p>
</li>
</ul>
<p>Specify version requirements for other Perl modules in the
pre-requisites in your Makefile.PL or Build.PL.</p>
<p>Be sure to specify Perl version requirements both in Makefile.PL or
Build.PL and with <code class="inline"><a class="l_k" href="functions/require.html">require</a> <span class="v">5.6.1</span></code>
or similar. See the section on
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">VERSION</span></code>
of <a href="functions/require.html">require</a> for details.</p>
<a name="Testing"></a><h2>Testing</h2>
<p>All modules should be tested before distribution (using "make disttest"),
and the tests should also be available to people installing the modules
(using "make test").
For Module::Build you would use the <code class="inline"><span class="w">make</span> <span class="w">test</span></code>
equivalent <code class="inline"><span class="w">perl</span> <span class="w">Build</span> <span class="w">test</span></code>
.</p>
<p>The importance of these tests is proportional to the alleged stability of a
module. A module which purports to be
stable or which hopes to achieve wide
use should adhere to as strict a testing regime as possible.</p>
<p>Useful modules to help you write tests (with minimum impact on your
development process or your time) include Test::Simple, Carp::Assert
and Test::Inline.
For more sophisticated test suites there are Test::More and Test::MockObject.</p>
<a name="Packaging"></a><h2>Packaging</h2>
<p>Modules should be packaged using one of the standard packaging tools.
Currently you have the choice between ExtUtils::MakeMaker and the
more platform independent Module::Build, allowing modules to be installed in a
consistent manner.
When using ExtUtils::MakeMaker, you can use "make dist" to create your
package. Tools exist to help you to build your module in a
MakeMaker-friendly style. These include ExtUtils::ModuleMaker and h2xs.
See also <a href="perlnewmod.html">perlnewmod</a>.</p>
<a name="Licensing"></a><h2>Licensing</h2>
<p>Make sure that your module has a license, and that the full text of it
is included in the distribution (unless it's a common one and the terms
of the license don't require you to include it).</p>
<p>If you don't know what license to use, dual licensing under the GPL
and Artistic licenses (the same as Perl itself) is a good idea.
See <a href="perlgpl.html">perlgpl</a> and <a href="perlartistic.html">perlartistic</a>.</p>
<a name="COMMON-PITFALLS"></a><h1>COMMON PITFALLS</h1>
<a name="Reinventing-the-wheel"></a><h2>Reinventing the wheel</h2>
<p>There are certain application spaces which are already very, very well
served by CPAN. One example is templating systems, another is date and
time modules, and there are many more. While it is a rite of passage to
write your own version of these things, please consider carefully
whether the Perl world really needs you to publish it.</p>
<a name="Trying-to-do-too-much"></a><h2>Trying to do too much</h2>
<p>Your module will be part of a developer's toolkit. It will not, in
itself, form the <b>entire</b> toolkit. It's tempting to add extra features
until your code is a monolithic system rather than a set of modular
building blocks.</p>
<a name="Inappropriate-documentation"></a><h2>Inappropriate documentation</h2>
<p>Don't fall into the trap of writing for the wrong audience. Your
primary audience is a reasonably experienced developer with at least
a moderate understanding of your module's application domain, who's just
downloaded your module and wants to start using it as quickly as possible.</p>
<p>Tutorials, end-user documentation, research papers, FAQs etc are not
appropriate in a module's main documentation. If you really want to
write these, include them as sub-documents such as <code class="inline"><span class="w">My::Module::Tutorial</span></code>
or
<code class="inline"><span class="w">My::Module::FAQ</span></code>
and provide a link in the SEE ALSO section of the
main documentation.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<ul>
<li><a name="the-perlstyle-manpage"></a><b><a href="perlstyle.html">perlstyle</a></b>
<p>General Perl style guide</p>
</li>
<li><a name="the-perlnewmod-manpage"></a><b><a href="perlnewmod.html">perlnewmod</a></b>
<p>How to create a new module</p>
</li>
<li><a name="the-perlpod-manpage"></a><b><a href="perlpod.html">perlpod</a></b>
<p>POD documentation</p>
</li>
<li><a name="the-podchecker-manpage"></a><b><a href="podchecker.html">podchecker</a></b>
<p>Verifies your POD's correctness</p>
</li>
<li><a name="Packaging-Tools"></a><b>Packaging Tools</b>
<p><a href="ExtUtils/MakeMaker.html">ExtUtils::MakeMaker</a>, <a href="http://search.cpan.org/perldoc/Module::Build">Module::Build</a></p>
</li>
<li><a name="Testing-tools"></a><b>Testing tools</b>
<p><a href="Test/Simple.html">Test::Simple</a>, <a href="http://search.cpan.org/perldoc/Test::Inline">Test::Inline</a>, <a href="http://search.cpan.org/perldoc/Carp::Assert">Carp::Assert</a>, <a href="Test/More.html">Test::More</a>, <a href="http://search.cpan.org/perldoc/Test::MockObject">Test::MockObject</a></p>
</li>
<li><a name="http%3a%2f%2fpause.perl.org%2f"></a><b><a href="http://pause.perl.org/">http://pause.perl.org/</a></b>
<p>Perl Authors Upload Server. Contains links to information for module
authors.</p>
</li>
<li><a name="Any-good-book-on-software-engineering"></a><b>Any good book on software engineering</b>
</li>
</ul>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Kirrily "Skud" Robert <skud@cpan.org></p>
<div id="page_index" class="hud_container">
<div id="page_index_header" class="hud_header">
<div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
<div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
<div id="page_index_topright" class="hud_topright"></div>
</div>
<div id="page_index_content" class="hud_content">
<ul><li><a href="#NAME">NAME</a><li><a href="#INTRODUCTION">INTRODUCTION</a><li><a href="#QUICK-CHECKLIST">QUICK CHECKLIST</a><ul><li><a href="#Before-you-start">Before you start</a><li><a href="#The-API">The API</a><li><a href="#Stability">Stability</a><li><a href="#Documentation">Documentation</a><li><a href="#Release-considerations">Release considerations</a></ul><li><a href="#BEFORE-YOU-START-WRITING-A-MODULE">BEFORE YOU START WRITING A MODULE</a><ul><li><a href="#Has-it-been-done-before%3f">Has it been done before?</a><li><a href="#Do-one-thing-and-do-it-well">Do one thing and do it well</a><li><a href="#What's-in-a-name%3f">What's in a name?</a><li><a href="#Get-feedback-before-publishing">Get feedback before publishing</a></ul><li><a href="#DESIGNING-AND-WRITING-YOUR-MODULE">DESIGNING AND WRITING YOUR MODULE</a><ul><li><a href="#To-OO-or-not-to-OO%3f">To OO or not to OO?</a><li><a href="#Designing-your-API">Designing your API</a><li><a href="#Strictness-and-warnings">Strictness and warnings</a><li><a href="#Backwards-compatibility">Backwards compatibility</a><li><a href="#Error-handling-and-messages">Error handling and messages</a></ul><li><a href="#DOCUMENTING-YOUR-MODULE">DOCUMENTING YOUR MODULE</a><ul><li><a href="#POD">POD</a><li><a href="#README%2c-INSTALL%2c-release-notes%2c-changelogs">README, INSTALL, release notes, changelogs</a></ul><li><a href="#RELEASE-CONSIDERATIONS">RELEASE CONSIDERATIONS</a><ul><li><a href="#Version-numbering">Version numbering</a><li><a href="#Pre-requisites">Pre-requisites</a><li><a href="#Testing">Testing</a><li><a href="#Packaging">Packaging</a><li><a href="#Licensing">Licensing</a></ul><li><a href="#COMMON-PITFALLS">COMMON PITFALLS</a><ul><li><a href="#Reinventing-the-wheel">Reinventing the wheel</a><li><a href="#Trying-to-do-too-much">Trying to do too much</a><li><a href="#Inappropriate-documentation">Inappropriate documentation</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a></ul>
</div>
<div id="page_index_footer" class="hud_footer">
<div id="page_index_bottomleft" class="hud_bottomleft"></div>
<div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="page_index_resize" class="hud_resize"></div>
</div>
</div>
</div>
<div id="content_footer">
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
<div id="footer_strapline">
perldoc.perl.org - Official documentation for the Perl programming language
</div>
<div id="footer_links">
<div id="address">
<p class="name">Contact details</p>
<p class="address">
Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
</p>
<p class="contact">
Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
</p>
</div>
<ul class="f1">
<li>Manual
<ul class="f2">
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">Changes</a>
</ul>
<li>Reference
<ul class="f2">
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Variables</a>
</ul>
<li>Modules
<ul class="f2">
<li><a href="index-modules-A.html">Modules</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
</ul>
<li>Misc
<ul class="f2">
<li><a href="index-licence.html">License</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platforms</a>
</ul> </ul>
<div class="clear"></div>
</div>
</div>
<div id="footer_end">
</div>
</div>
</div>
<script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
<script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(0);
perldoc.pageName = 'perlmodstyle';
perldoc.pageAddress = 'perlmodstyle.html';
perldoc.contentPage = 1;
explorePerl.render();
explorePerl.addEvents('explore_anchor');
</script>
</body>
</html>
|