Bukowski has asked for the wisdom of the Perl Monks concerning the following question:

Dear PerlMonks,

I guess there comes a time in every perl newbies life that the directory full of scripts that has accumulated over the last X months would be better off in a module.

Unfortunately my copy of Learning Perl (2nd edition) doesn't quite cover this.

I've been brave enough to peer into modules (once or twice) but its still a little arcane for me. What good resources exist to smooth this task for me? Links, hints, LARTs all welcome.

Humbly yours,

Bukowski - aka Dan (dcs@black.hole-in-the.net)
"Coffee for the mind, Pizza for the body, Sushi for the soul" -Userfriendly

Replies are listed 'Best First'.
Re: Module writing hints?
by Zaxo (Archbishop) on Jun 27, 2002 at 17:07 UTC
Re: Module writing hints?
by kvale (Monsignor) on Jun 27, 2002 at 17:07 UTC
    Starting small is not too hard. Here is module Foo.pm:
    package Foo; require Exporter; @ISA = qw|Exporter|; @EXPORT_OK = qw|foo|; sub foo { print "Hello World\n"; } 1;
    And here is how it is used:
    use Foo foo; foo(); print "Goodbye\n";
    Simplicity itself: we mark the routine  foo() as exportable and import it with  use Foo foo;.

    The complexity you saw comes if you want to package up your module as a full blown library worthy of CPAN. For that you will want to read perlman:perlmod to see how modules work and perlman:perlmodlib to see how to create them. Also check out perlnewmod for preparing a new module for distribution.

Re: Module writing hints?
by samtregar (Abbot) on Jun 27, 2002 at 17:10 UTC
Re: Module writing hints?
by mfriedman (Monk) on Jun 27, 2002 at 16:44 UTC
    There are a few things you should consider. Is this going to be an OO class? Or just a module with functions? Will it export functions and variables? The most complete doc I know of is perlmod.

    If you're going to be doing OO stuff, you'll want to read perlboot and perltoot also.

Re: Module writing hints?
by Aristotle (Chancellor) on Jun 27, 2002 at 17:13 UTC

    In its simplest form, a module is nothing other than a regular Perl source that has a package Module::Name; somewhere at the top, ends with a statement that returns true (usually a simple 1;) and resides in a file called Module/Name.pm. :)

    It really isn't very daunting. Of course, these few rules alone won't make it a good module, or even a well behaved one, but as with most things in Perl, good behaviour is a matter of conventions and social rituals rather than something the language shoves down your throat. perlmod and perlmodlib should show you the ropes.

    Makeshifts last the longest.