summaryrefslogtreecommitdiff
path: root/posts/2009-05-22-modules-i-like---module-setup.org
blob: 2bbca9f8ce0dac28b19aa3c138d6c02f9b3884cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
[[http://search.cpan.org/perldoc?Module::Setup][Module::Setup]] by
[[http://blog.yappo.jp/][Yappo]] is a really nice module. I don't like
[[http://search.cpan.org/perldoc?Module::Starter][Module::Starter]],
it's not easy to create template to make it do what you need. With
Module::Setup you can create flavors for any type of modules you want.
Most of the modules I create for work use Moose, and I like to use
Test::Class too. I've created a Moose flavor for creating this kind of
modules.

*** Creating a Moose flavor for Module::Setup

First, you tell it to init a new flavor:

#+BEGIN_EXAMPLE
    module-setup --init moose
#+END_EXAMPLE

Module::Setup ask what is your favorite SCM. For me, it's git. It will
create files in /$HOME/.module-setup/flavors/moose//.

Start by editing
*$HOME/.module-setup/flavors/moose/template/lib/\_\_\_\_var-module\_path-var\_\_\_\_.pm*
to make it look like this

#+BEGIN_SRC perl
    - use strict;
    - use warnings;
    + use Moose;
#+END_SRC

Add *requires 'Moose'* in *Makefile.PL*. Create a
**t/tests/Test/\_\_\_\_var-module\_path-var\_\_\_\_.pm** file with the
following content:

#+BEGIN_SRC perl
    package Test :: [%module %];

    use strict;
    use warnings;
    use base 'Test::Class';
    use Test::Exception;
    use Test::More;

    sub class {'[% module %]'}

    sub startup : Tests(startup => 1) {
        my $test = shift;
        use_ok $test->class, "use ok";
    }

    sub shutdown : Tests(shutdown) {
        my $test = shift;
    }

    sub constructor : Tests(1) {
        my $test = shift;
        can_ok $test->class, 'new';
    }

    1;
#+END_SRC

You will have a Test::Class test ready with basic tests already
implemented.

If you want to share this template at $work, easy:

#+BEGIN_EXAMPLE
    module-setup --pack DevMoose moose > DevMoose.pm
#+END_EXAMPLE

You just have to send DevMoose.pm to who need it, and he will be able to
import it with

#+BEGIN_EXAMPLE
    module-setup --init --flavor-class=+DevMoose moose
#+END_EXAMPLE

Now you can create a new module

#+BEGIN_EXAMPLE
    module-setup MY::AWESOME::MODULE moose
#+END_EXAMPLE