views:

91

answers:

4

I work as an ETL developer and I have been meaning to create a Perl module where I can put a lot of the subroutines. I would also like, if it is possible, to have the module handle all the logging & error reporting although the second one is more of a nice to have than an immediate need.

I have never created a module or package in Perl before and I was wondering if folks can point me to tutorials & other reference materials that guides me through the process of taking a whole bunch of subroutines/functions that I have in my scripts and turning that into a module/package.

Thanks, Ashish

+1  A: 

For example:

package MyModule;

use warnings;
use strict;

use Exporter 'import';

sub func_a { ... }

sub func_b { ... }

# ...

# Don't forget to return a true value so use will succeed!
1;

Now other modules can say

use MyModule;

print func_a(), func_b(), "\n";
Greg Bacon
Thanks for the example.
tundal45
+5  A: 

Here are some free online resources to create Perl modules:

They also have links to other resources.

toolic
tundal45
+1  A: 

Browse the source code of modules on your system or on CPAN for good ideas and module design and implementation that you can steal borrow.

If you use cpan or perl -MCPAN ... to install modules on your own system, spend some time browsing your own <cpanhome>/build directory to get the gist of how the modules are built, tested, and installed on your system.

mobrule
Thanks mobrule. Currently, I am trying to make the subroutines as decoupled as I can so I am sure design ideas from other modules would be helpful. I need to use more hashes as parameters so I can add flexibility.
tundal45
+1  A: 

Most of what I know about modules, I learned from O'Reilly's The Perl Cookbook, in particular, there is good coverage of h2xs, an executable which ships with Perl which builds a scaffold module, including test directories and Makefile.pl, a makefile generator.

Barton Chittenden
Thanks for the correction tsee.
Barton Chittenden