Recall.pm

filter: Statementstmt 94.5% 69 / 73 Branchbran 75.0% 24 / 32 Conditioncond 46.6% 7 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 9 / 9 Podpod 60.0% 3 / 5 totaltotal 78.4% 113 / 144 CC 23 SCAR 21.4
CC
23
Cov
83%
new
16.1
trim
4.1
init_template
4.0
CRAP 8.5
↑ Summary Recall/Base.pm »
1 package Template::Recall;
2
3 6 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 6 ▶use base qw(Template::Recall::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 # This version: only single file template or template string
10 6 our $VERSION='0.18';
11
12
13 sub new {
14
15 5 ▶ my $class = shift;
Subroutine new: called CC=13 SCAR=27.8
Pod documented
16 5 my $self = {};
17
18 5 my ( %h ) = @_;
19
20 # Set default values
21 5 $self->{'is_file_template'} = 0;
22 5 $self->{'template_secpat'} = qr/\[\s*=+\s*\w+\s*=+\s*\]/; # Section pattern
23 5 $self->{'secpat_delims'} = [ '\[\s*=+\s*', '\s*=+\s*\]' ]; # Section delimiters
24 5 $self->{'val_delims'} = [ '\[\'', '\'\]' ];
25 5 $self->{'trim'} = undef; # undef=off
26 #TODO remove?
27 5 $self->{'stored_secs'} = {}; # Store rendered sections internally
28
29
30 5 bless( $self, $class );
31
32 # User defines section pattern
33 5 ▶ $self->{'template_secpat'} = qr/$h{'secpat'}/ if defined( $h{'secpat'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"secpat"} 1 4 5
34
35 # Section: User sets 'no delimiters'
36 $self->{'secpat_delims'} = undef
37 5 ▶ if defined($h{'secpat_delims'}) and !ref($h{'secpat_delims'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"secpat_delims"} and not ref $h{"secpat_delims"} 1 4 5
Condition66%2/3
defined $h{"secpat_delims"} and not ref $h{"secpat_delims"}
!ll&&!rl&&r
401
MC/DC50%1/2
defined $h{"secpat_delims"} and not ref $h{"secpat_delims"}
defined $h{"secpat_delims"} not ref $h{"secpat_delims"}
Truth table66%2/3
A and not(B)
A= defined $h{"secpat_delims"}B= not ref $h{"secpat_delims"}
ABresult
0X0
100
111
38
39 # Section: User specifies delimiters
40 0 $self->{'secpat_delims'} = [ @{ $h{'secpat_delims'} } ]
41 5 ▶ if defined($h{'secpat_delims'}) and ref($h{'secpat_delims'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"secpat_delims"} and ref $h{"secpat_delims"} 0 5 5
Condition66%2/3
defined $h{"secpat_delims"} and ref $h{"secpat_delims"}
!ll&&!rl&&r
410
MC/DC0%0/2
defined $h{"secpat_delims"} and ref $h{"secpat_delims"}
defined $h{"secpat_delims"} ref $h{"secpat_delims"}
Truth table66%2/3
A and B
A= defined $h{"secpat_delims"}B= ref $h{"secpat_delims"}
ABresult
0X0
100
111
42
43
44 # User sets 'no delimiters'
45 5 ▶ $self->{'val_delims'} = undef if defined($h{'val_delims'}) and !ref($h{'val_delims'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"val_delims"} and not ref $h{"val_delims"} 0 5 5
Condition33%1/3
defined $h{"val_delims"} and not ref $h{"val_delims"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $h{"val_delims"} and not ref $h{"val_delims"}
defined $h{"val_delims"} not ref $h{"val_delims"}
Truth table33%1/3
A and not(B)
A= defined $h{"val_delims"}B= not ref $h{"val_delims"}
ABresult
0X0
100
111
46
47 # User specifies delimiters
48 0 $self->{'val_delims'} = [ @{ $h{'val_delims'} } ]
49 5 ▶ if defined($h{'val_delims'}) and ref($h{'val_delims'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"val_delims"} and ref $h{"val_delims"} 0 5 5
Condition33%1/3
defined $h{"val_delims"} and ref $h{"val_delims"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $h{"val_delims"} and ref $h{"val_delims"}
defined $h{"val_delims"} ref $h{"val_delims"}
Truth table33%1/3
A and B
A= defined $h{"val_delims"}B= ref $h{"val_delims"}
ABresult
0X0
100
111
50
51 # User supplied the template from a string
52
53 5 ▶ if ( defined($h{'template_str'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h{'template_str'}) { } 2 3 5
54 2 $self->init_template($h{'template_str'});
55 2 return $self;
56 }
57 else {
58 3 ▶ die if ( not defined($h{'template_path'}) or !-e $h{'template_path'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{"template_path"} and -e $h{"template_path"} 0 3 3
Condition33%1/3
defined $h{"template_path"} and -e $h{"template_path"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $h{"template_path"} and -e $h{"template_path"}
defined $h{"template_path"} -e $h{"template_path"}
Truth table33%1/3
A and B
A= defined $h{"template_path"}B= -e $h{"template_path"}
ABresult
0X0
100
111
59 }
60
61 3 $self->{'template_path'} = $h{'template_path'};
62 3 $self->init_template_from_file();
63
64 3 return $self;
65
66 } # new()
67
68
69
70
71
72
73 sub render {
74
75 12 ▶ my ( $self, $section, $hash_ref ) = @_;
Subroutine render: called CC=3 SCAR=11.3
Pod documented
76
77 12 ▶ return "Error: no section to render: $section\n" if !defined($section);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section 0 12 12
78
79 12 ▶ return if (!exists $self->{'template_secs'}->{$section});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"template_secs"}{$section} 0 12 12
80
81 12 my $sectemp = $self->{'template_secs'}->{$section};
82
83 #TODO is it faster if the default delim allows for *no* spaces between ID
84 12 return $self->SUPER::render( $sectemp, $hash_ref, $self->{'val_delims'} );
85
86 } # render()
87
88
89
90
91
92
93 # Load the single file template into array of sections
94
95 sub init_template_from_file {
96
97 3 ▶ my $self = shift;
Subroutine init_template_from_file: called CC=3 SCAR=11.0
Pod undocumented
98
99 3 my $t;
100 3 ▶ open my $fh, $self->{'template_path'} or die "Couldn't open $self->{'template_path'} $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $self->{"template_path"} or die qq[Couldn't open $self->{"template_path"} $!] 0 3 3
101 3 ▶ while(<$fh>) { $t .= $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 12 3 15
102 3 close $fh;
103 3 $self->init_template($t);
104
105 } # init_file_template()
106
107
108
109
110
111 # Handle template passed by user as string
112
113 sub init_template {
114
115 5 ▶ my ($self, $template) = @_;
Subroutine init_template: called CC=4 SCAR=13.9
Pod undocumented
116
117 5 my $sec = [ split( /($self->{'template_secpat'})/, $template ) ];
118
119 5 my %h;
120 5 my $curr = '';
121
122 # top-down + only one 'body' follows section, why this parse hack works
123 5 for (@$sec) {
124 23 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 5 18 23
125 18 ▶ if (/$self->{'template_secpat'}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$self->{'template_secpat'}/) { } 9 9 18
126 9 $curr = $_;
127 9 $curr =~ s/$self->{'secpat_delims'}[0]|$self->{'secpat_delims'}[1]//g;
128 9 $h{$curr} = '';
129 }
130 else {
131 9 $h{$curr} = $_;
132 }
133 }
134
135 5 $self->{'template_secs'} = \%h;
136
137 }
138
139
140
141
142
143
144 # Set trim flags
145 sub trim {
146 4 ▶ my ($self, $flag) = @_;
Subroutine trim: called CC=4 SCAR=14.0
Pod documented
147
148
149
150 # trim() with no params defaults to trimming both ends
151 4 ▶ if (!defined($flag)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flag) 1 3 4
152 1 $self->{'trim'} = 'both';
153 1 return;
154 }
155
156
157
158 # Turn trimming off
159 3 ▶ if ($flag =~ /^(off|o)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag =~ /^(off|o)$/i) 1 2 3
160 1 $self->{'trim'} = undef;
161 1 return;
162 }
163
164
165 # Make sure we get something valid
166 2 ▶ if ($flag !~ /^(off|left|right|both|l|r|b|o)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag =~ /^(off|left|right|both|l|r|b|o)$/i) 0 2 2
167 0 $self->{'trim'} = undef;
168 0 return;
169 }
170
171
172 2 $self->{'trim'} = $flag;
173 2 return;
174
175
176 } # trim()
177
178
179
180 6 1;
181
182
183 __END__
↑ Summary Recall/Base.pm »