Declare.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 62.5% 10 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 89.4% 93 / 104 CC 12 SCAR 11.1
CC
12
Cov
90%
_build_group
6.1
__ANON__
4.0
__ANON__
3.1
CRAP 3.0
↑ Summary Declare/LibXML.pm »
1 package Tree::Template::Declare;
2 {
3 6 $Tree::Template::Declare::DIST = 'Tree-Template-Declare';
4 }
5 6 $Tree::Template::Declare::VERSION = '0.7';
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Sub::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Devel::Caller 'caller_args';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 {
14 6 my $exporter=Sub::Exporter::build_exporter({
15 groups => {
16 default => \&_build_group,
17 },
18 });
19  
20 8 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
21 8 my ($pack,@rest)=@_;
22  
23 8 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 8 0 8
24 8 @_=($pack,-default => {@rest});
25 }
26 8 goto $exporter;
27 }
28 }
29  
30 6 our @nodes_stack;
31  
32 8 ▶sub _build_group {
Subroutine _build_group: called CC=6 SCAR=18.1
33 8 my ($class,$name,$args,$coll)=@_;
34  
35 8 my $builder=$args->{builder};
36  
37 8 ▶ if (! ref $builder) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $builder) 8 0 8
38 8 my $builder_pkg=$builder;
39 8 ▶ if ($builder_pkg=~m{\A [+](\w+) \z}smx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builder_pkg =~ /\A [+](\w+) \z/msx) 8 0 8
40 8 $builder_pkg="Tree::Template::Declare::$1";
41 }
42 8 ▶ eval "require $builder_pkg" ## no critic (ProhibitStringyEval)
Branch: 1 branch
BranchTrueFalseTotal
eval "require $builder_pkg" or croak("Can't load ${builder_pkg}: $@") 8 0 8
43 or croak "Can't load $builder_pkg: $@"; ## no critic (ProhibitPunctuationVars)
44  
45 8 ▶ if ($builder_pkg->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builder_pkg->can('new')) { } 8 0 8
46 8 $builder=$builder_pkg->new();
47 }
48 else {
49 0 $builder=$builder_pkg;
50 }
51 }
52  
53 my $normal_exports= {
54 9 ▶ tree => sub(&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 9 my $tree=$builder->new_tree();
56  
57 9 unshift @nodes_stack,$tree;
58 9 $_[0]->(caller_args(1));
59 9 shift @nodes_stack;
60  
61 9 return $builder->finalize_tree($tree);
62 },
63 41 ▶ node => sub (&) {
Subroutine __ANON__: called CC=4 SCAR=13.9
64 41 my $node=$builder->new_node();
65  
66 41 unshift @nodes_stack, $node;
67 41 $_[0]->(caller_args(1));
68 41 shift @nodes_stack;
69  
70 41 ▶ my $scalar_context=defined wantarray && !wantarray;
Condition66%2/3
defined wantarray && !wantarray
!ll&&!rl&&r
3803
MC/DC50%1/2
defined wantarray && !wantarray
defined wantarray !wantarray
Truth table66%2/3
A && not(B)
A= defined wantarrayB= !wantarray
ABresult
0X0
100
111
71 41 ▶ if (@nodes_stack && !$scalar_context) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Tree::Template::Declare::nodes_stack and not $scalar_context) 38 3 41
Condition66%2/3
@Tree::Template::Declare::nodes_stack and not $scalar_context
!ll&&!rl&&r
0338
MC/DC50%1/2
@Tree::Template::Declare::nodes_stack and not $scalar_context
@Tree::Template::Declare::nodes_stack not $scalar_context
Truth table66%2/3
A and not(B)
A= @Tree::Template::Declare::nodes_stackB= not $scalar_context
ABresult
0X0
100
111
72 38 $builder->add_child_node($nodes_stack[0],$node);
73 }
74 41 return $node;
75 },
76 1 ▶ attach_nodes => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
77 1 ▶ if (@nodes_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Tree::Template::Declare::nodes_stack) 1 0 1
78 1 for my $newnode (@_) {
79 3 $builder->add_child_node($nodes_stack[0],
80 $newnode);
81 }
82 }
83 },
84 41 ▶ name => sub ($) {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 41 $builder->set_node_name($nodes_stack[0],$_[0]);
86 41 return;
87 },
88 23 ▶ attribs => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 23 my %attrs=@_;
90 23 $builder->set_node_attributes($nodes_stack[0],\%attrs);
91 23 return;
92 },
93 3 ▶ detached => sub($) { return scalar $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 8 };
95 8 ▶ if ($builder->can('_munge_exports')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builder->can('_munge_exports')) { } 3 5 8
96 3 return $builder->_munge_exports($normal_exports,\@nodes_stack);
97 }
98 else {
99 5 return $normal_exports;
100 }
101 }
102  
103 6 1;
104  
105 =pod
106  
107 =encoding UTF-8
108  
109 =head1 NAME
110  
111 Tree::Template::Declare - easily build tree structures
112  
113 =head1 VERSION
114  
115 version 0.7
116  
117 =head1 SYNOPSIS
118  
119 use Tree::Template::Declare builder => '+DAG_Node';
120  
121 my $tree=tree {
122 node {
123 name 'root';
124 attribs name => 'none';
125 node {
126 name 'first';
127 attribs name => 'number_1';
128 attribs other => 'some';
129 };
130 node {
131 name 'second';
132 };
133 };
134 };
135  
136 =head1 FUNCTIONS
137  
138 For details on the implementation of these functions, see the
139 L</BUILDER> section, and the documentation of your chosen builder.
140  
141 =head2 C<tree>
142  
143 This function takes a code ref or a block, inside which calls to
144 C<node> should be made, and returns a properly constructed tree
145 containing those nodes.
146  
147 Uses the builder's C<new_tree> and C<finalize_tree>.
148  
149 =head2 C<node>
150  
151 This function takes a code ref or a block, inside which calls to
152 C<name>, C<attribs>, and C<node> should be made, and returns the node.
153  
154 If I<not> called in scalar context, it also adds the node to the
155 "calling" node or tree.
156  
157 Uses the builder's C<new_node> and C<add_child_node>.
158  
159 =head2 C<detached>
160  
161 Alias for C<scalar>, so that you can say C<return detached node ...>
162 without having to worry about the calling context.
163  
164 =head2 C<attach_nodes>
165  
166 This function takes a list of nodes, and adds them (in order) to the
167 "calling" node or tree. You should only use this with nodes you
168 obtained by calling C<node> in scalar context.
169  
170 Uses the builder's C<add_child_node>.
171  
172 =head2 C<name>
173  
174 This function takes a scalar, and sets the name of the current node to
175 the value of that scalar.
176  
177 Uses the builder's C<set_node_name>.
178  
179 =head2 C<attribs>
180  
181 This function takes a hash (not a hash ref), and sets the attributes
182 of the current node.
183  
184 Uses the builder's C<set_node_attributes>.
185  
186 =head1 BUILDER
187  
188 To actually create nodes and trees, this module uses helper classes
189 called "builders". You must always specify a builder package, class or
190 object with the C<builder> option in the C<use> line.
191  
192 If the builder is an object, the methods discussed below will be
193 called on it; if it's a class (i.e. a package that has a C<new>
194 function), they will be called on an instance created by calling
195 C<new> without parameters; otherwise they will be called as class
196 methods.
197  
198 The builder must implement these methods:
199  
200 =over
201  
202 =item C<new_tree>
203  
204 $tree = $current_node = $builder->new_tree();
205  
206 returns a tree object; that object will be set as the current node
207 within the code passed to the C<tree> function
208  
209 =item C<finalize_tree>
210  
211 return $builder->finalize_tree($tree);
212  
213 this function will be passed the object returned by C<new_tree>, after
214 the code passed to C<tree> has been executed; the result of
215 C<finalize_tree> will be the result of C<tree>
216  
217 =item C<new_node>
218  
219 $current_node=$builder->new_node();
220  
221 returns a new, unattached node
222  
223 =item C<set_node_name>
224  
225 $builder->set_node_name($current_node, $name);
226  
227 sets the name of the node (e.g. for SGML-like trees, this is the "tag
228 name")
229  
230 =item C<set_node_attributes>
231  
232 $builder->set_node_attributes($current_node, \%attribs);
233  
234 sets attributes of the node; it should not remove previously-set attributes
235  
236 =item C<add_child_node>
237  
238 $builder->add_child_node($parent_node, $child_node);
239  
240 adds the second node at the end of the children list of the first node
241  
242 =back
243  
244 The builder can also implement an C<_munge_exports> method. If it
245 does, C<_munge_exports> will be called with:
246  
247 =over 4
248  
249 =item *
250  
251 a hash ref consisting of the functions that C<Tree::Template::Declare>
252 wants to export,
253  
254 =item *
255  
256 an array ref, whose first element will be the current node whenever
257 the user calls an exported function
258  
259 =back
260  
261 C<_munge_exports> should return a hash ref with the functions that
262 will actually be exported.
263  
264 See L<Sub::Exporter>, in particular the section on group builders, for
265 details. See L<Tree::Template::Declare::HTML_Element> and
266 L<Tree::Template::Declare::LibXML> for examples.
267  
268 =head1 IMPORTING
269  
270 This module uses L<Sub::Exporter>, although it munges the C<use> list
271 before passing it to L<Sub::Exporter>. A line like:
272  
273 use Tree::Template::Declare @something;
274  
275 becomes a call to L<Sub::Exporter>'s export sub like:
276  
277 $export->('Tree::Template::Declare',-default => {@something});
278  
279 See L<Sub::Exporter>'s documentation for things like renaming the
280 imports.
281  
282 You can C<use> this module more than once, with different builders and
283 different names for the imports:
284  
285 use Tree::Template::Declare -prefix=> 'x', builder => '+LibXML';
286 use Tree::Template::Declare -prefix=> 'd', builder => '+DAG_Node';
287  
288 =head1 KNOWN ISSUES & BUGS
289  
290 =over 4
291  
292 =item *
293  
294 C<_munge_exports> is ugly
295  
296 =item *
297  
298 the context-sensitivity of C<node> might not be the best way to DWIM
299 for the creation of detached nodes
300  
301 =back
302  
303 =head1 AUTHOR
304  
305 Gianni Ceccarelli <dakkar@thenautilus.net>
306  
307 =head1 COPYRIGHT AND LICENSE
308  
309 This software is copyright (c) 2015 by Gianni Ceccarelli <dakkar@thenautilus.net>.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as the Perl 5 programming language system itself.
313  
314 =cut
315  
316 __END__
↑ Summary Declare/LibXML.pm »