Extension.pm

filter: Statementstmt 91.8% 146 / 159 Branchbran 64.0% 32 / 50 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 24 / 26 Podpod 62.5% 5 / 8 totaltotal 82.6% 209 / 253 CC 33 SCAR 16.7
CC
33
Cov
84%
_loadFile
20.2
tmplfile
5.3
output
5.0
CRAP 5.3
↑ Summary Extension/HEAD_BODY.pm »
1 package HTML::Template::Pro::Extension;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use integer; # no floating point math so far!
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::Template::Pro;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base "HTML::Template::Pro";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 $HTML::Template::Pro::Extension::VERSION = "0.11";
12 0 ▶sub Version { $HTML::Template::Pro::Extension::VERSION; }
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
13  
14 1 my $fields = {
15 tmplfile => '',
16 source => undef,
17 source_orig => undef,
18 plugins => [] ,
19 __plugins => {},
20 __reloadSource => 0,
21 __reloadFile => 0,
22 };
23  
24 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
25 2 my $proto = shift;
26 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
27 # valid HTML::Template::Pro parameter
28 2 my $htpoptions={
29 functions => {},
30 debug => 0,
31 max_includes => 10,
32 global_vars => 0,
33 no_includes => 0,
34 search_path_on_include => 0,
35 loop_context_vars => 1,
36 path => [],
37 associate => [],
38 case_sensitive => 0,
39 strict => 1,
40 die_on_bad_params => 0,
41 scalarref => '',
42 option => 'value'
43 };
44 # set the htp options with new parameters
45 2 my %opt = @_;
46 2 foreach (keys %$htpoptions) {
47 28 ▶ $htpoptions->{$_} = $opt{$_} if (exists $opt{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{$_} 0 28 28
48 }
49 # carico il modulo HTML::Template::Pro
50 2 my $self = {%$fields,%{new HTML::Template::Pro(%$htpoptions)}};
51 2 bless $self,$class;
52 2 $self->_init(@_);
53 2 return $self;
54 }
55  
56 2 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
57 2 my $self = shift;
58 2 my %opts = @_;
59 2 ▶ $self->tmplfile($opts{tmplfile}) if ($opts{tmplfile});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"tmplfile"} 2 0 2
60 2 foreach (@{$opts{plugins}}) {
61 1 $self->plugin_add($_);
62 }
63 }
64  
65 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
66  
67 17 ▶sub tmplfile {
Subroutine tmplfile: called CC=5 SCAR=16.7
Pod documented
68 17 my $self = shift;
69 17 ▶ if (my $tmplfile = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmplfile = shift()) 17 0 17
70 17 $self->{__reloadFile} = 1;
71 17 ▶ unless (ref($tmplfile) && $self->{tmplfile}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tmplfile and $self->{"tmplfile"}) 17 0 17
Condition33%1/3
ref $tmplfile and $self->{"tmplfile"}
!ll&&!rl&&r
1700
MC/DC0%0/2
ref $tmplfile and $self->{"tmplfile"}
ref $tmplfile $self->{"tmplfile"}
Truth table33%1/3
A and B
A= ref $tmplfileB= $self->{"tmplfile"}
ABresult
0X0
100
111
72 # sia $tmplfile che $self->{tmplfile} sono stringhe
73 17 ▶ if ($tmplfile eq $self->{tmplfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmplfile eq $self->{"tmplfile"}) 6 11 17
74 # il tmplfile e' lo stesso, niente reload
75 6 $self->{__reloadFile} = 0;
76 }
77 }
78 17 $self->{tmplfile} = $tmplfile;
79 }
80 17 return $self->{tmplfile};
81 }
82  
83 18 ▶sub output {
Subroutine output: called CC=5 SCAR=16.2
Pod documented
84 # redefine standard output function
85 18 my $self = shift;
86 18 my %args = @_;
87 18 ▶ $self->_reloadFile if ($self->{__reloadFile});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__reloadFile"} 11 7 18
88 18 ▶ $self = $self->_reloadSource if ($self->{__reloadSource});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__reloadSource"} 14 4 18
89 18 ▶ if (exists $args{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"as"}) 15 3 18
90 # delete old params settings
91 # I don't know if this is a bug or a change in H::T::Pro interface
92 # however
93 15 ▶ if ($self->can('clear_params')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('clear_params')) { } 15 0 15
94 15 $self->clear_params();
95 } else {
96 0 $self->clear_param();
97 }
98 15 $self->param(%{$args{as}});
99 }
100 18 return $self->SUPER::output(print_to => $args{print_to});
101 }
102  
103 14 ▶sub html {
Subroutine html: called CC=3 SCAR=11.3
Pod documented
104 14 my $self = shift;
105 14 ▶ my %args = (defined $_[0]) ? %{$_[0]} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 14 0 14
106 14 ▶ $self->tmplfile($_[1]) if (defined $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 14 0 14
107 14 return $self->output('as' => \%args);
108 }
109  
110 11 ▶sub _reloadFile {
Subroutine _reloadFile: called CC=1 SCAR=0.0
111 11 my $self = shift;
112 11 $self->_loadFile;
113 11 $self->{__reloadSource} = 1;
114 }
115  
116 11 ▶sub _loadFile {
Subroutine _loadFile: called CC=8 SCAR=30.1
117 11 my $self = shift;
118 11 ▶ if (ref($self->{tmplfile}) eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $self->{'tmplfile'} eq '') { } 11 0 11
elsif (ref $self->{'tmplfile'} eq 'SCALAR') { } 0 0 0
elsif (ref $self->{'tmplfile'} eq 'ARRAY') { } 0 0 0
elsif (ref $self->{'tmplfile'} eq 'GLOB') { } 0 0 0
119 11 my $filepath = $self->_find_file($self->{tmplfile});
120 11 ▶ confess("HTML::Template->new() : Cannot open included file $self->{tmplfile} : file not found.") unless defined($filepath);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filepath 0 11 11
121 # we'll need this for future reference - to call stat() for example.
122 # read into scalar
123 11 ▶ confess("HTML::Template::Pro::Extension : Cannot open included file $self->{tmplfile} : $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined open(TEMPLATE, $filepath) 0 11 11
124 unless defined(open(TEMPLATE, $filepath));
125 11 $self->{source_orig} = "";
126 11 ▶ while (read(TEMPLATE, $self->{source_orig}, 10240, length($self->{source_orig}))) {}
Branch: 1 branch
BranchTrueFalseTotal
if (read TEMPLATE, $self->{"source_orig"}, 10240, length $self->{"source_orig"}) 11 11 22
127 11 close(TEMPLATE);
128 } elsif (ref($self->{tmplfile}) eq 'SCALAR') {
129 # copy in the template text
130 0 $self->{source_orig} = ${$self->{tmplfile}};
131 0 delete($self->{tmplfile});
132 } elsif (ref($self->{tmplfile}) eq 'ARRAY') {
133 # if we have an array ref, join and store the template text
134 0 $self->{source_orig} = join("", @{$self->{tmplfile}});
135 0 delete($self->{tmplfile});
136 } elsif (ref($self->{tmplfile}) eq 'GLOB') {
137 # just read everything in in one go
138 0 local $/ = undef;
139 0 $self->{source_orig} = readline($self->{tmplfile});
140 0 delete($self->{tmplfile});
141 } else {
142 0 confess("HTML::Template::Pro::Extension : Need to set file with a filename, filehandle, scalarref or arrayref parameter specified.");
143 }
144 11 $self->{__reloadFile} = 0;
145 }
146  
147 14 ▶sub _reloadSource {
Subroutine _reloadSource: called CC=4 SCAR=13.9
148 14 my $self = shift;
149 14 my $src_orig = $self->{source_orig};
150 14 foreach my $plugin (values %{$self->{__plugins}}) {
151 29 foreach my $filter (@{$plugin->{filter}}) {
152 38 ▶ croak("HTML::Template::Pro::Extension : bad value set for filter parameter - must be a code ref or a hash ref.") unless ref $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filter 0 38 38
153 38 &$filter(\$src_orig,$self);
154 }
155 }
156 14 $self->{source} = $src_orig;
157
158 # ricarico il modulo HTML::Template::Pro
159 14 $self->{scalarref} = $src_orig;
160 14 $self->{options}->{scalarref} = $src_orig;
161 14 $self->{__reloadSource} = 0;
162 14 return $self;
163 }
164  
165 10 ▶sub plugin_add {
Subroutine plugin_add: called CC=2 SCAR=7.0
Pod undocumented
166 10 my $s = shift;
167 10 my ($module, $module_name) = $s->_module_info(shift);
168 # plugin gia caricato
169 10 ▶ return if (exists $s->{__plugins}->{$module_name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"__plugins"}{$module_name} 0 10 10
170 10 $s->{__plugins}->{$module_name}->{obj} = $module;
171 # init module
172 10 $s->_initModule($module);
173 # add filter from added module to me
174 10 $s->_pushModule($module);
175 10 $s->{__reloadSource} = 1;
176 }
177  
178 10 ▶sub _initModule {
Subroutine _initModule: called CC=2 SCAR=6.9
179 10 my $self = shift;
180 10 my $module = shift;
181 10 ▶ if (ref($module) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $module eq '') { } 9 1 10
182 9 $self->_importModule($module);
183 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
184 9 &{$module . "::init"}($self);
185 } else {
186 1 $module->init($self);
187 }
188 }
189  
190 9 ▶sub _importModule {
Subroutine _importModule: called CC=1 SCAR=0.0
191 9 my $self = shift;
192 9 my $module_name = shift;
193  
194 9 $module_name =~s/::/\//g;
195 9 require $module_name . ".pm";
196 }
197  
198  
199 10 ▶sub _pushModule {
Subroutine _pushModule: called CC=2 SCAR=6.9
200 10 my $self = shift;
201 10 my ($module, $module_name) = $self->_module_info(shift);
202 10 my @codes;
203 10 ▶ if (ref($module) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $module eq '') { } 9 1 10
204 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
205 9 @codes = &{$module . "::get_filter"}($self);
206 } else {
207 1 @codes = $module->get_filter($self);
208 }
209 10 $self->{__plugins}->{$module_name}->{filter} = \@codes;
210 }
211  
212 1 ▶sub plugin_remove {
Subroutine plugin_remove: called CC=1 SCAR=0.0
Pod undocumented
213 1 my $s = shift;
214 1 my ($module, $module_name) = $s->_module_info(shift);
215 #delete $s->{plugins}->{$module_name};
216 1 delete $s->{__plugins}->{$module_name};
217 1 $s->{__reloadSource} = 1;
218 }
219  
220 3 ▶sub plugins_clear {
Subroutine plugins_clear: called CC=1 SCAR=0.0
Pod undocumented
221 3 my $s = shift;
222 3 $s->{plugins} = [];
223 3 $s->{__plugins} = {};
224 3 $s->{__reloadSource} = 1;
225 }
226  
227 21 ▶sub _module_info {
Subroutine _module_info: called CC=3 SCAR=11.0
228 21 my $self = shift;
229 21 my $module = shift;
230 21 my $module_name;
231 21 ▶ if (ref($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $module) { } 2 19 21
232 2 $module_name = ref($module);
233 } else {
234 19 ▶ $module = "HTML::Template::Pro::Extension::$module"
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /::/ 10 9 19
235 if ($module!~/::/);
236 19 $module_name = $module;
237 }
238 21 return ($module,$module_name);
239 }
240  
241 9 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
242 9 my $self = shift;
243 9 my @procs = split(/::/,$HTML::Template::Pro::Extension::AUTOLOAD);
244 #confess("Unable to find $HTML::Template::Pro::Extension::AUTOLOAD") if (scalar(@procs)<4);
245 9 my $proc = $procs[-1];
246 9 my $value;
247 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
248 9 foreach my $module (keys %{$self->{__plugins}}) {
249 27 my $ret;
250 27 $ret= eval { return &{"${module}::$proc"}($self,@_) };
251 27 ▶ if (!$@) { return $ret };
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 9 18 27
252 };
253 0 confess("Unable to find $HTML::Template::Pro::Extension::AUTOLOAD");
254 }
255  
256 1 1;
257  
258 # vim: set ts=2:
↑ Summary Extension/HEAD_BODY.pm »