blib/lib/Data/Template.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 96.6% 87 / 90 CC 10 SCAR 9.1
CC
10
Cov
96%
process
4.0
process_s
3.0
_split_scalar
3.0
CRAP 2.5
↑ Summary
1  
2 package Data::Template;
3  
4 =encoding utf8
5 =cut
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.02';
11  
12 4 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 Data::Template->mk_accessors(qw(engine prefix));
14  
15 4 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 2 my $self = shift;
19 2 my $TT = Template->new(INTERPOLATE => 1);
20 # return $self->SUPER::new({engine => $TT, prefix => '=', @_});
21 2 return $self->SUPER::new({engine => $TT, @_});
22 }
23  
24 28 ▶sub process {
Subroutine process: called CC=4 SCAR=14.0
Pod documented
25 28 my $self = shift;
26 28 my $tt = shift;
27 28 my $vars = shift;
28  
29 28 ▶ if (!ref $tt) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $tt) { } 22 6 28
elsif (ref $tt eq 'ARRAY') { } 4 2 6
elsif (ref $tt eq 'HASH') { } 2 0 2
30 22 return $self->process_s($tt, $vars);
31 } elsif (ref $tt eq 'ARRAY') {
32 4 return $self->process_a($tt, $vars);
33 } elsif (ref $tt eq 'HASH') {
34 2 return $self->process_h($tt, $vars);
35 } else {
36 0 die 'burp'
37 }
38 }
39  
40 2 ▶sub process_h {
Subroutine process_h: called CC=2 SCAR=6.9
Pod documented
41 2 my $self = shift;
42 2 my $h = shift;
43 2 my $vars = shift;
44  
45 2 my %ph = ();
46 2 ▶ while (my ($k, $v) = each %$h) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h) 8 2 10
47 8 $k = $self->process_s($k, $vars);
48 8 $v = $self->process($v, $vars);
49 8 $ph{$k} = $v;
50 }
51 2 return \%ph;
52 }
53  
54 4 ▶sub process_a {
Subroutine process_a: called CC=2 SCAR=6.9
Pod documented
55 4 my $self = shift;
56 4 my $a = shift;
57 4 my $vars = shift;
58  
59 4 my @pa;
60 4 foreach (@$a) {
61 12 push @pa, $self->process($_, $vars);
62 }
63 4 return \@pa;
64 }
65  
66 =begin private
67  
68 ($p, $t) = $self->_split_scalar($s)
69  
70 Determines if C<$s> is a plain scalar or
71 a text template. If it is a plain scalar,
72 C<$p> gets its content and C<$t> gets C<undef>.
73 Otherwise, C<$p> is C<undef> and C<$t>
74 gets the contents of the template.
75  
76 There is difference between C<$s> and
77 the content of C<$s> only if there is a
78 C<prefix>. In this case, a plain scalar
79 is (1) one which does not begin with the prefix
80 (the content is C<$s> itself) or (2)
81 one which begins with C<"\\"> followed
82 by the prefix (the content is C<$s> without
83 the leading escape). When there is
84 a prefix, C<$s> is a template if it begins
85 with the prefix and its content is C<$s>
86 without the prefix.
87  
88 # say prefix is '='
89 ($p, $t) = $tt->_split_scalar('foo')
90 # ($p, $t) = ('foo', undef)
91 ($p, $t) = $tt->_split_scalar('=foo')
92 # ($p, $t) = (undef, 'foo')
93 ($p, $t) = $tt->_split_scalar('\\=foo')
94 # ($p, $t) = ('=foo', undef)
95  
96 =end private
97  
98 =cut
99  
100 34 ▶sub _split_scalar {
Subroutine _split_scalar: called CC=3 SCAR=11.0
101 34 my $self = shift;
102 34 my $s = shift;
103 34 my $prefix = $self->prefix;
104 34 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 17 17 34
105 17 ▶ if ($s =~ s/^\Q$prefix\E//) { # it is a template
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/^\Q$prefix\E//) { } 4 13 17
106 4 return (undef, $s);
107 } else {
108 13 $s =~ s/^\\(\Q$prefix\E)/$1/; # chomp the leading escape
109 13 return ($s, undef);
110 }
111 }
112 # by now everything else looks like a template
113 17 return (undef, $s);
114 }
115  
116 34 ▶sub process_s {
Subroutine process_s: called CC=3 SCAR=11.0
Pod documented
117 34 my $self = shift;
118 34 my $s = shift;
119 34 my $vars = shift;
120  
121 34 my ($p, $t) = $self->_split_scalar($s);
122 34 ▶ return $p if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 13 21 34
123  
124 21 my $ps;
125 21 ▶ $self->engine->process(\$t, $vars, \$ps)
Branch: 1 branch
BranchTrueFalseTotal
$self->engine->process(\$t, $vars, \$ps) or die $self->engine->error 21 0 21
126 or die $self->engine->error();
127 21 return $ps;
128
129  
130 }
131  
132 4 1;
133  
134 __END__
↑ Summary