Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Tutorial on making a proper module?

by Cody Fendant (Hermit)
on Jan 14, 2016 at 00:05 UTC ( [id://1152728]=perlquestion: print w/replies, xml ) Need Help??

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

I've created a module for my own purposes to access a particular SOAP interface.

All I've really done is make some .pm files which do what I want.

What are the steps I should follow if I want to make it a proper installable module with tests, which cpan etc. would place in the system path? Which tests should I write, at a minimum? Is there a good tutorial on this which Monks recommend?

Followup question: there are non-perl files (XML Templates) used by the module. What's the right way to place those files in order for the module to find them? Can I just put them in a templates directory inside the module's directory?

Replies are listed 'Best First'.
Re: Tutorial on making a proper module?
by stevieb (Canon) on Jan 14, 2016 at 00:23 UTC

    choroba has (or been party to helping) put this together. Check it out and see if it works for you.

    As far as the templates, yes, put them in a templates directory. If you need them installed as part of your *distribution* (that's what we call a CPAN package), personally, I'd use the Makefile (or Build file) to drop them into the appropriate location.

    Head on over to MetaCPAN and take a look at the structure of some of the distributions. You'll see structure, test layout etc. Reviewing ones that the Testers link states is passing most (if not all) the tests is a good way to understand a properly structured and designed dist. I'd recommend to look at one of mine, but that'd be biased. Data::Compare is one that may help, that has a decent structure for a newb to CPAN to look at. It does not have extra directories (such as templates), but I think it'll help you get you on your way.

    I'll let the other CPAN Monks speak up now.

      That tutorial is very good, thanks a lot for that. I will definitely follow it.

      I have one specific question about the templates thing still unanswered though. Say I have a module like this, and there's an essential file right there in the same directory as TestModule.pm

      package TestModule; open( FILE, '<', 'essential_file.txt' ) or die("Can't open file"); 1;

      and I run it like this:

      require TestModule; print "hello world";

      It will fail (unless the script is in the same directory as TestModule).

      I'm probably missing something obvious but how do I get the file path right so that TestModule.pm can find its file, no matter where it's called from?

Re: Tutorial on making a proper module?
by toolic (Bishop) on Jan 14, 2016 at 00:54 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found