The Perl POD is a little "head first" with respect to introducing modules. perlmod is not a tutorial. This is where a book like Intermediate Perl (Published by O'Reilly) is very helpful. It will walk you through the basics of modules, and then take you into more advanced topics such as putting together a module distribution (tests and all).

But the basic idea is that you create a module in its own file. It has its own namespace. You can choose to export some of its functions into the caller's namespace, or make exporting optional. You can write a functional interface, or an object oriented interface, or a combination of both. Within the calling script, you use ModuleName;, along with an import list if necessary. Then within the calling script, you can use those functions (or classes/objects) without copying and pasting every time you want their functionality.

there are lightweight techniques too, but they're rarely used nowadays in well-written code; techniques such as reading in a file of code and evaling it, or do Fildname;, and others. But in this day and age, such techniques are not really considered suitable 99.9% of the time. I mention them only because they show up in older code from time to time, and you may see them.

Grab a copy of Intermediate Perl at your favorite bookstore. If you intend to become effective in using Perl, it's $20 dollars well spent.


Dave


In reply to Re: Building in a Subroutine? by davido
in thread Building in a Subroutine? by perl.j

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.