File Coverage

blib/lib/Dist/Zilla/Plugin/TemplateModule.pm
Criterion Covered Total %
statement 33 34 97.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::TemplateModule 6.037;
2             # ABSTRACT: a simple module-from-template plugin
3              
4 2     2   1605 use Moose;
  2         6  
  2         21  
5             with 'Dist::Zilla::Role::ModuleMaker',
6             'Dist::Zilla::Role::TextTemplate';
7              
8 2     2   18820 use Dist::Zilla::Pragmas;
  2         6  
  2         20  
9              
10 2     2   21 use Dist::Zilla::Path;
  2         4  
  2         25  
11              
12 2     2   851 use namespace::autoclean;
  2         6  
  2         23  
13              
14 2     2   182 use autodie;
  2         19  
  2         61  
15              
16 2     2   15016 use Sub::Exporter::ForMethods;
  2         6  
  2         38  
17             use Data::Section 0.200002 # encoding and bytes
18 2         23 { installer => Sub::Exporter::ForMethods::method_installer },
19 2     2   667 '-setup';
  2         91  
20 2     2   3252 use Dist::Zilla::File::InMemory;
  2         22  
  2         1074  
21              
22             #pod =head1 MINTING CONFIGURATION
23             #pod
24             #pod This module is part of the standard configuration of the default L<Dist::Zilla>
25             #pod Minting Profile, and all profiles that don't set a custom ':DefaultModuleMaker'
26             #pod so you don't need to normally do anything to configure it.
27             #pod
28             #pod dzil new Some::Module
29             #pod # creates ./Some-Module/*
30             #pod # creates ./Some-Module/lib/Some/Module.pm
31             #pod
32             #pod However, for those who wish to configure this ( or any subclasses ) this is
33             #pod presently required:
34             #pod
35             #pod [TemplateModule / :DefaultModuleMaker]
36             #pod ; template = SomeFile.pm
37             #pod
38             #pod =head1 DESCRIPTION
39             #pod
40             #pod This is a L<ModuleMaker|Dist::Zilla::Role::ModuleMaker> used for creating new
41             #pod Perl modules files when minting a new dist with C<dzil new>. It uses
42             #pod L<Text::Template> (via L<Dist::Zilla::Role::TextTemplate>) to render a template
43             #pod into a Perl module. The template is given two variables for use in rendering:
44             #pod C<$name>, the module name; and C<$dist>, the Dist::Zilla object. The module is
45             #pod always created as a file under F<./lib>.
46             #pod
47             #pod By default, the template looks something like this:
48             #pod
49             #pod use strict;
50             #pod use warnings;
51             #pod package {{ $name }};
52             #pod
53             #pod 1;
54             #pod
55             #pod =attr template
56             #pod
57             #pod The C<template> parameter may be given to the plugin to provide a different
58             #pod filename, absolute or relative to the build/profile directory.
59             #pod
60             #pod If this parameter is not specified, this module will use the boilerplate module
61             #pod template included in this module.
62             #pod
63             #pod =cut
64              
65             has template => (
66             is => 'ro',
67             isa => 'Str',
68             predicate => 'has_template',
69             );
70              
71             sub make_module {
72 2     2 0 7 my ($self, $arg) = @_;
73              
74 2         7 my $template;
75              
76 2 50       112 if ($self->has_template) {
77 0         0 $template = path( $self->template )->slurp_utf8;
78             } else {
79 2         5 $template = ${ $self->section_data('Module.pm') };
  2         17  
80             }
81              
82             my $content = $self->fill_in_string(
83             $template,
84             {
85             dist => \($self->zilla),
86             name => $arg->{name},
87             },
88 2         778 );
89              
90 2         45 my $filename = $arg->{name} =~ s{::}{/}gr;
91              
92 2         138 my $file = Dist::Zilla::File::InMemory->new({
93             name => "lib/$filename.pm",
94             content => $content,
95             });
96              
97 2         20 $self->add_file($file);
98             }
99              
100             __PACKAGE__->meta->make_immutable;
101             1;
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             Dist::Zilla::Plugin::TemplateModule - a simple module-from-template plugin
110              
111             =head1 VERSION
112              
113             version 6.037
114              
115             =head1 DESCRIPTION
116              
117             This is a L<ModuleMaker|Dist::Zilla::Role::ModuleMaker> used for creating new
118             Perl modules files when minting a new dist with C<dzil new>. It uses
119             L<Text::Template> (via L<Dist::Zilla::Role::TextTemplate>) to render a template
120             into a Perl module. The template is given two variables for use in rendering:
121             C<$name>, the module name; and C<$dist>, the Dist::Zilla object. The module is
122             always created as a file under F<./lib>.
123              
124             By default, the template looks something like this:
125              
126             use strict;
127             use warnings;
128             package {{ $name }};
129              
130             1;
131              
132             =head1 PERL VERSION
133              
134             This module should work on any version of perl still receiving updates from
135             the Perl 5 Porters. This means it should work on any version of perl
136             released in the last two to three years. (That is, if the most recently
137             released version is v5.40, then this module should work on both v5.40 and
138             v5.38.)
139              
140             Although it may work on older versions of perl, no guarantee is made that the
141             minimum required version will not be increased. The version may be increased
142             for any reason, and there is no promise that patches will be accepted to
143             lower the minimum required perl.
144              
145             =head1 ATTRIBUTES
146              
147             =head2 template
148              
149             The C<template> parameter may be given to the plugin to provide a different
150             filename, absolute or relative to the build/profile directory.
151              
152             If this parameter is not specified, this module will use the boilerplate module
153             template included in this module.
154              
155             =head1 MINTING CONFIGURATION
156              
157             This module is part of the standard configuration of the default L<Dist::Zilla>
158             Minting Profile, and all profiles that don't set a custom ':DefaultModuleMaker'
159             so you don't need to normally do anything to configure it.
160              
161             dzil new Some::Module
162             # creates ./Some-Module/*
163             # creates ./Some-Module/lib/Some/Module.pm
164              
165             However, for those who wish to configure this ( or any subclasses ) this is
166             presently required:
167              
168             [TemplateModule / :DefaultModuleMaker]
169             ; template = SomeFile.pm
170              
171             =head1 AUTHOR
172              
173             Ricardo SIGNES 😏 <cpan@semiotic.systems>
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2026 by Ricardo SIGNES.
178              
179             This is free software; you can redistribute it and/or modify it under
180             the same terms as the Perl 5 programming language system itself.
181              
182             =cut
183              
184             __DATA__
185             __[ Module.pm ]__
186             use strict;
187             use warnings;
188             package {{ $name }};
189              
190             1;