Recall.pm

filter: Statementstmt 94.0% 63 / 67 Branchbran 75.0% 21 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 60.0% 3 / 5 totaltotal 79.8% 99 / 124 SCAR 29.7
CC
18
Cov
84%
new
10.2
trim
4.1
init_template
4.0
CRAP 19.4
↑ Summary Recall/Base.pm »
1 package Template::Recall;
2
3 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 4 ▶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 4 our $VERSION='0.19';
11
12
13 sub new {
14
15 3 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=23.2
Pod documented
16 3 my $self = {};
17 3 bless $self, $class;
18
19 3 my ( %h ) = @_;
20
21 # Default values
22 3 $self->{'secpat'} = qr/\[\s*=+\s*(\w+)\s*=+\s*\]/;
23 3 $self->{'val_delims'} = [ qr/\['/, qr/'\]/ ];
24 3 $self->{'trim'} = undef; # undef=off
25
26
27 3 ▶ if (exists $h{'secpat'} and ref $h{'secpat'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h{"secpat"} and ref $h{"secpat"}) 0 3 3
Condition33%1/3
exists $h{"secpat"} and ref $h{"secpat"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $h{"secpat"} and ref $h{"secpat"}
exists $h{"secpat"} ref $h{"secpat"}
Truth table33%1/3
A and B
A= exists $h{"secpat"}B= ref $h{"secpat"}
ABresult
0X0
100
111
28 0 $self->{'secpat'} = $h{'secpat'};
29 }
30
31 3 ▶ if (exists $h{'val_delims'} and ref $h{'val_delims'} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h{"val_delims"} and ref $h{"val_delims"} eq "ARRAY") 0 3 3
Condition33%1/3
exists $h{"val_delims"} and ref $h{"val_delims"} eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
exists $h{"val_delims"} and ref $h{"val_delims"} eq "ARRAY"
exists $h{"val_delims"} ref $h{"val_delims"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $h{"val_delims"}B= ref $h{"val_delims"} eq "ARRAY"
ABresult
0X0
100
111
32 0 $self->{'val_delims'} = $h{'val_delims'};
33 }
34
35 # User supplied the template from a string
36
37 3 ▶ if ( defined($h{'template_str'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h{"template_str"}) 2 1 3
38 2 $self->init_template($h{'template_str'});
39 2 return $self;
40 }
41
42 die 'Path to template not defined or missing'
43 1 ▶ unless defined($h{'template_path'}) and -e $h{'template_path'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{"template_path"} and -e $h{"template_path"} 0 1 1
Condition33%1/3
defined $h{"template_path"} and -e $h{"template_path"}
!ll&&!rl&&r
001
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
44
45
46 1 $self->init_template_from_file($h{'template_path'});
47
48 1 return $self;
49
50 } # new()
51
52
53 sub init_template_from_file {
54
55 1 ▶ my ($self, $tpath) = @_;
Subroutine init_template_from_file: called CC=3 SCAR=11.0
Pod undocumented
56
57 1 my $s;
58 1 ▶ open my $fh, $tpath or die "Couldn't open $tpath: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $tpath or die "Couldn't open ${tpath}: $!" 0 1 1
59 1 ▶ while(<$fh>) { $s .= $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 1 3
60 1 close $fh;
61 1 $self->init_template($s);
62
63 }
64
65 # Handle template passed by user as string
66 sub init_template {
67
68 3 ▶ my ($self, $template) = @_;
Subroutine init_template: called CC=4 SCAR=13.9
Pod undocumented
69
70 3 my $sec = [ split( /($self->{'secpat'})/, $template ) ];
71
72 3 my %h;
73 3 my $curr = '';
74
75 # Top-down + only one 'body' follows section, why this parse hack works
76 3 ▶ for (my $i=0; $i <= $#{$sec} ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$sec;}) 13 3 16
77 13 my $el = $$sec[$i];
78 13 ▶ next if $el =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $el =~ /^$/ 3 10 13
79 10 ▶ if ($el =~ /$self->{'secpat'}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ /$self->{'secpat'}/) { } 5 5 10
80 5 $curr = $1;
81 5 $h{$curr} = '';
82 5 $i++; # Skip next, it's the section name (an artifact)
83 }
84 else {
85 5 $h{$curr} = $el;
86 }
87 }
88
89 3 $self->{'template_secs'} = \%h;
90
91 } # init_template()
92
93
94 sub render {
95
96 8 ▶ my ( $self, $section, $hash_ref ) = @_;
Subroutine render: called CC=3 SCAR=11.4
Pod documented
97
98 8 ▶ die "Error: no section to render: $section\n" if !defined($section);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section 0 8 8
99
100 8 ▶ return if !exists $self->{'template_secs'}->{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"template_secs"}{$section} 0 8 8
101
102 return $self->SUPER::render(
103 8 $self->{'template_secs'}->{$section}, $hash_ref, $self->{'val_delims'});
104
105 } # render()
106
107
108 # Set trim flags
109 sub trim {
110 4 ▶ my ($self, $flag) = @_;
Subroutine trim: called CC=4 SCAR=14.0
Pod documented
111
112 # trim() with no params defaults to trimming both ends
113 4 ▶ if (!defined $flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flag) 1 3 4
114 1 $self->{'trim'} = 'both';
115 1 return;
116 }
117
118 # Turn trimming off
119 3 ▶ if ($flag =~ /^(off|o)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag =~ /^(off|o)$/i) 1 2 3
120 1 $self->{'trim'} = undef;
121 1 return;
122 }
123
124 # Make sure we get something valid
125 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
126 0 $self->{'trim'} = undef;
127 0 return;
128 }
129
130 2 $self->{'trim'} = $flag;
131 2 return;
132
133
134 } # trim()
135
136
137
138 4 1;
139
140
141 __END__
↑ Summary Recall/Base.pm »