Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Module writing hints?

by Bukowski (Deacon)
on Jun 27, 2002 at 16:30 UTC ( [id://177753]=perlquestion: print w/replies, xml ) Need Help??

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.

    -Mark
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177753]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found