One point that needs clarifying, that is not touched on in Ovid's or tilly's reply, is the definition of the term "module". A loose definition of a module, as a collection of related subroutines is useful for resolving the question, but may lead to confusion if you get deeper into the subject.

The loose definition includes modules (*.pm), libraries, packages and distributions.

libraries

Putting your common reusable subroutines in a .pl file, a Perl library, is the traditional perl 4 way of doing things. Such libraries are loaded in at run time, using require 'foo.pl'.

packages

Suppose you are coding a Perl script that defines a subroutine called load_data. You are using the library common.pl which is a collective effort, and you don't know everything that it has inside. In particular, you don't know that your colleague Fred already has a sub called load_data.

Packages avoid this problem by providing separate namespaces. Hence we might have Application::load_data and Reference::load_data as distinct, separate subroutines. So, this involves more typing, but provides a software development model which works better (the extra typing may be avoided by importing, see below).

modules

Perl 5 introduced the concept of Perl module (.pm) files, which may be "require"d at run time, but are more often "use"d at compile time (BEGIN time). Modules usually declare their own package namespace, but sometimes .pm files may populate more than one namespace.

Importing of subroutine names is also available with "use".

use MyModule::Clever qw(foo bar) ... my $wangle = foo('fleeg') . bar('dingle');

In tbe "use" declaration, we have told MyModule::Clever that we want to import the subroutines foo and bar into our namespace.

Modules can also provide an object orientated interface, but this is beyond the scope of this reply.

distributions

The tar ball kit that one downloads from CPAN is sometimes referred to as a module, but the correct term for this is a distribution. Distributions contain one or more modules, and also unit tests, documentation, example scripts, etc.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)


In reply to Re: Subroutines vs Modules by rinceWind
in thread Subroutines vs Modules by sub_chick

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.