blib/lib/Templ/Spec.pm

filter: Statementstmt 84.5% 180 / 213 Branchbran 54.2% 38 / 70 Conditioncond 44.4% 12 / 27 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 85.3% 35 / 41 Podpod 0.0% 0 / 16 totaltotal 69.5% 267 / 384 CC 47 SCAR 17.7
CC
47
Cov
74%
_load_subclass
18.4
templ
10.1
_ext_caller
7.6
CRAP 5.9
« blib/lib/Templ/Tag.pm ↑ Summary Build »
1 package Templ::Spec;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Templ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use overload '""' => \&as_perl, '&{}' => \&as_sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my $PKG = __PACKAGE__;
15  
16 sub _subclass_loaded ($) {
17 11 ▶ my $class = shift;
Subroutine _subclass_loaded: called CC=3 SCAR=11.0
18 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 my %pkg = %{$class.'::'};
20 11 ▶ return 1 if exists $pkg{'TEMPL_TAGS'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pkg{"TEMPL_TAGS"} 9 2 11
21 2 ▶ return 1 if exists $pkg{'TEMPL_HEADERS'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pkg{"TEMPL_HEADERS"} 0 2 2
22 2 return 0;
23 }
24  
25 # Resolves, checks and loads a subclass of Templ::Spec
26 sub _load_subclass ($) {
27 9 ▶ my $class = shift;
Subroutine _load_subclass: called CC=10 SCAR=29.1
28 9 my $caller = $class.'->'.caller();
29 9 ▶ defined($class)
Branch: 1 branch
BranchTrueFalseTotal
defined $class or croak("Undefined class in call to $caller") 0 9 9
30 || croak "Undefined class in call to $caller";
31 9 ▶ ref($class) && $class->isa($PKG)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class->isa($PKG) and croak("Class method $caller called as object method") 0 9 9
Condition33%1/3
ref $class and $class->isa($PKG)
!ll&&!rl&&r
900
MC/DC0%0/2
ref $class and $class->isa($PKG)
ref $class $class->isa($PKG)
Truth table33%1/3
A and B
A= ref $classB= $class->isa($PKG)
ABresult
0X0
100
111
32 && croak "Class method $caller called as object method";
33 9 ▶ ref($class)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and croak("Unknown parameter found in class position for $caller") 0 9 9
34 && croak "Unknown parameter found in class position for $caller";
35 9 ▶ $class =~ m/^(\w+\:\:)*\w+$/
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^(\w+\:\:)*\w+$/ or croak("Invalid first string parameter: not a class for $caller") 0 9 9
36 || croak "Invalid first string parameter: not a class for $caller";
37 9 ▶ $class eq $PKG
Branch: 1 branch
BranchTrueFalseTotal
$class eq $PKG and croak("$PKG must be subclassed!") 0 9 9
38 && croak "$PKG must be subclassed!";
39 9 ▶ unless ( _subclass_loaded($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_subclass_loaded($class)) 0 9 9
40 0 eval "require $class;";
41 0 ▶ unless ( _subclass_loaded($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_subclass_loaded($class)) 0 0 0
42 0 croak "Unable to load $PKG subclass $class (have you not added headers or tags?)";
43 }
44 }
45 9 ▶ $class->isa($PKG)
Branch: 1 branch
BranchTrueFalseTotal
$class->isa($PKG) or croak("Class $class does not inherit from $PKG") 0 9 9
46 || croak "Class $class does not inherit from $PKG";
47 9 return $class;
48 }
49  
50 # Attempts to determine the non-Templ context from which a call came
51 sub _ext_caller () {
52 2 ▶ my $context = 0;
Subroutine _ext_caller: called CC=7 SCAR=20.2
53 2 ▶ while ( $context <= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context <= 5) 5 0 5
54 5 my @info = caller($context);
55 5 ▶ my $pkg = defined($info[0]) ? $info[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $info[0] ? : 5 0 5
56 5 ▶ next if !$pkg || $pkg eq 'Templ::Spec' || $pkg eq 'Templ';
Branch: 1 branch
BranchTrueFalseTotal
if not $pkg or $pkg eq "Templ::Spec" or $pkg eq "Templ" 3 2 5
Condition83%5/6
not $pkg or $pkg eq "Templ::Spec" or $pkg eq "Templ"
l!l&&r!l&&!r
212
not $pkg or $pkg eq "Templ::Spec"
l!l&&r!l&&!r
023
MC/DC66%2/3
not $pkg or $pkg eq "Templ::Spec" or $pkg eq "Templ"
not $pkg $pkg eq "Templ::Spec" $pkg eq "Templ"
Truth table75%3/4
not(A) or B or C
A= not $pkgB= $pkg eq "Templ::Spec"C= $pkg eq "Templ"
ABCresult
1XX1
01X1
0011
0000
57 2 ▶ return wantarray ? @info : $pkg;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
58 3 } continue { ++$context }
59 0 croak "Deep frame stack for _ext_caller";
60 }
61  
62 sub _load_templ_code_from_file ($$) {
63 2 ▶ my $self = shift;
Subroutine _load_templ_code_from_file: called CC=6 SCAR=19.2
64 2 my $fh;
65 2 ▶ if (not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0]) { } 1 1 2
66 # We were passed a filename
67 1 my $filename = shift;
68 1 ▶ croak "Unable to stat file '$filename'" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 1 1
69 1 $fh = IO::File->new($filename, 'r');
70 1 ▶ defined($fh) || croak "Unable to open file ".$filename.": $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or croak('Unable to open file ' . $filename . ": $!") 0 1 1
71 }
72 else {
73 # We were passed a filehandle
74 1 $fh = shift;
75 }
76 2 ▶ unless ( openhandle($fh) || eval { $fh->can('getline') } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&openhandle($fh) or eval { do { $fh->can("getline") } }) 0 2 2
Condition33%1/3
&openhandle($fh) or eval { do { $fh->can("getline") } }
l!l&&r!l&&!r
200
MC/DC0%0/2
&openhandle($fh) or eval { do { $fh->can("getline") } }
&openhandle($fh) eval { do { $fh->can("getline") } }
Truth table33%1/3
A or B
A= &openhandle($fh)B= eval { do { $fh->can("getline") } }
ABresult
1X1
011
000
77 0 croak "$PKG filehandle parameter doesn't behave like a filehandle";
78 }
79 2 local $/ = undef;
80 2 $self->{'templ_code'} = <$fh>;
81 2 close $fh;
82 }
83  
84 ##############################################################################
85 # Class methods
86 #
87 # These use used by consumers of Templ::Spec's (packages and other code)
88 # in order to instantiate templates, or import methods which in turn
89 # instantiate templates
90  
91 sub templ {
92 5 ▶ my $class = _load_subclass(shift @_); # Might be a partial class if called from get()
Subroutine templ: called CC=8 SCAR=23.2
Pod undocumented
93 5 my $self = bless {}, $class;
94 5 ▶ if (scalar(@_) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 4 1 5
elsif (scalar @_ == 2) { } 1 0 1
95 4 ▶ my $source = shift || croak "Must provide source for ".caller();
Condition33%1/3
shift() || croak('Must provide source for ' . (caller()))
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() || croak('Must provide source for ' . (caller()))
shift() croak('Must provide source for ' . (caller()))
Truth table33%1/3
A || B
A= shift()B= croak('Must provide source for ' . (caller()))
ABresult
1X1
011
000
96 4 ▶ if (ref $source) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source) { } 1 3 4
97 1 $self->_load_templ_code_from_file($source);
98 }
99 else {
100 3 $self->{'templ_code'} = $source;
101 }
102 }
103 elsif (scalar(@_) == 2) {
104 1 ▶ my $type = shift || croak "Must provide type for ".caller();
Condition33%1/3
shift() || croak('Must provide type for ' . (caller()))
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || croak('Must provide type for ' . (caller()))
shift() croak('Must provide type for ' . (caller()))
Truth table33%1/3
A || B
A= shift()B= croak('Must provide type for ' . (caller()))
ABresult
1X1
011
000
105 1 ▶ my $source = shift || croak "Must provide source for ".caller();
Condition33%1/3
shift() || croak('Must provide source for ' . (caller()))
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || croak('Must provide source for ' . (caller()))
shift() croak('Must provide source for ' . (caller()))
Truth table33%1/3
A || B
A= shift()B= croak('Must provide source for ' . (caller()))
ABresult
1X1
011
000
106 1 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'file') { } 1 0 1
107 1 $self->_load_templ_code_from_file($source);
108 }
109 else {
110 0 croak "Unknown $PKG source type $type";
111 }
112 }
113 else {
114 0 croak "Incorrect new $class parameters: ".Dumper(\@_);
115 }
116 5 return $self;
117 }
118  
119 # Alias ->templ() to ->new()
120 1 *new = *templ;
121  
122 sub templ_method ($$$;$) {
123 #print Dumper(\@_);
124 2 ▶ my $class = _load_subclass(shift @_);
Subroutine templ_method: called CC=1 SCAR=0.0
Pod undocumented
125 2 my $method_name = shift;
126 2 my $templ = $class->templ( @_ );
127 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 2 *{ _ext_caller().'::'.$method_name } = $templ->as_method();
129 2 return $templ;
130 }
131  
132 sub templ_sub ($$$;$) {
133 0 ▶ my $class = _load_subclass(shift @_);
Subroutine templ_sub: not called CC=1 SCAR=2.6
Pod undocumented
134 0 my $sub_name = shift;
135 0 my $templ = $class->templ( @_ );
136 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
137 0 *{ _ext_caller().'::'.$sub_name } = $templ->as_sub();
138 0 return $templ;
139 }
140  
141 ##############################################################################
142 # Hybrid class/object methods
143  
144 sub tags {
145 5 ▶ my $class = ref( $_[0] ) || $_[0];
Subroutine tags: called CC=5 SCAR=16.3
Pod undocumented
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
146 5 my @tags = ();
147 5 foreach my $this_class ( Class::ISA::self_and_super_path($class) ) {
148 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
149 20 ▶ next unless scalar( @{ $this_class.'::TEMPL_TAGS' } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$this_class . '::TEMPL_TAGS';} 8 12 20
150 12 push @tags, @{ $this_class.'::TEMPL_TAGS' };
151 }
152 5 ▶ return wantarray ? @tags : \@tags;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
153 }
154  
155 sub header {
156 5 ▶ my $class = ref( $_[0] ) || $_[0];
Subroutine header: called CC=4 SCAR=13.9
Pod undocumented
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
157 5 my @headers = ();
158 5 foreach my $this_class ( Class::ISA::self_and_super_path($class) ) {
159 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 20 ▶ next unless scalar( @{ $this_class.'::TEMPL_HEADERS' } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$this_class . '::TEMPL_HEADERS';} 14 6 20
161 6 push @headers, @{ $this_class.'::TEMPL_HEADERS' };
162 }
163 5 return join '', map {"$_\n"} @headers;
164 }
165  
166 ##############################################################################
167 # Object methods
168  
169 # Get the template contents of the object
170 sub templ_code {
171 5 ▶ my $self = shift;
Subroutine templ_code: called CC=1 SCAR=0.0
Pod undocumented
172 5 return $self->{'templ_code'};
173 }
174  
175 # Returns an eval-able string perl block which returns the output of the
176 # template
177 sub as_perl {
178 0 ▶ my $self = shift;
Subroutine as_perl: not called CC=2 SCAR=17.9
Pod undocumented
179 0 ▶ if ( not defined $self->{'as_perl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_perl"}) 0 0 0
180 0 $self->{'as_perl'} = '{'
181 . Templ::Parser::Return->new()->parse($self)
182 . '}';
183 }
184 0 return $self->{'as_perl'};
185 }
186  
187 # Returns an eval-able string perl block which returns the output of the
188 # template, with newline-spanning strings split into multiple perl code lines
189 sub as_pretty_perl {
190 0 ▶ my $self = shift;
Subroutine as_pretty_perl: not called CC=2 SCAR=17.9
Pod undocumented
191 0 ▶ if ( not defined $self->{'as_pretty_perl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_pretty_perl"}) 0 0 0
192 0 $self->{'as_pretty_perl'} = '{'
193 . Templ::Parser::Return->new( 'prettyify' => 1 )->parse($self)
194 . '}';
195 }
196 0 return $self->{'as_pretty_perl'};
197 }
198  
199 # Returns a code reference to a block-based handler for the template
200 sub as_sub {
201 3 ▶ my $self = shift;
Subroutine as_sub: called CC=3 SCAR=11.0
Pod undocumented
202 3 ▶ if ( not defined $self->{'as_sub'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_sub"}) 2 1 3
203 2 my $sub;
204 1 ▶ eval '$sub = sub {'
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
205 . Templ::Parser::Return->new()->parse($self)
206 . '}';
207 2 ▶ $@ && croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($@) 0 2 2
208 2 $self->{'as_sub'} = $sub;
209 }
210 3 return $self->{'as_sub'};
211 }
212  
213 # Returns a code reference to a block-based handler for the template
214 sub as_method {
215 2 ▶ my $self = shift;
Subroutine as_method: called CC=3 SCAR=11.0
Pod undocumented
216 2 ▶ if ( not defined $self->{'as_method'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_method"}) 2 0 2
217 2 my $method;
218 1 ▶ eval '$method = sub {'
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
219 . 'my $self = shift; '
220 . Templ::Parser::Return->new()->parse($self)
221 . '}';
222 2 ▶ $@ && croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($@) 0 2 2
223 2 $self->{'as_method'} = $method;
224 }
225 2 return $self->{'as_method'};
226 }
227  
228 # Runs the print handler on the passed params... in other words it executes
229 # the template in such a way that the output is sent to the select()ed FH
230 sub as_print {
231 0 ▶ my $self = shift;
Subroutine as_print: not called CC=2 SCAR=17.9
Pod undocumented
232 0 ▶ if ( not defined $self->{'as_print'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_print"}) 0 0 0
233 0 $self->{'as_print'} = '{'
234 . Templ::Parser::Print->new()->parse($self)
235 . '}';
236 }
237 0 return $self->{'as_print'};
238 }
239  
240 # Returns an eval-able string perl block which returns the output of the
241 # template, with newline-spanning strings split into multiple lines
242 sub as_pretty_print {
243 0 ▶ my $self = shift;
Subroutine as_pretty_print: not called CC=2 SCAR=17.9
Pod undocumented
244 0 ▶ if ( not defined $self->{'as_pretty_print'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_pretty_print"}) 0 0 0
245 0 $self->{'as_pretty_print'} = '{'
246 . Templ::Parser::Print->new( 'prettyify' => 1 )->parse($self)
247 . '}';
248 }
249 0 return $self->{'as_pretty_print'};
250 }
251  
252 # Returns a code reference to a printing handler for this template
253 sub as_print_sub {
254 1 ▶ my $self = shift;
Subroutine as_print_sub: called CC=3 SCAR=11.0
Pod undocumented
255 1 ▶ if ( not defined $self->{'as_print_sub'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_print_sub"}) 1 0 1
256 1 my $sub;
257 1 ▶ eval '$sub = sub {'
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
258 . Templ::Parser::Print->new()->parse($self)
259 . '}';
260 1 ▶ $@ && croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($@) 0 1 1
261 1 $self->{'as_print_sub'} = $sub;
262 }
263 1 return $self->{'as_print_sub'};
264 }
265  
266 # Runs the return handler on the passed params... in other words, executes
267 # the template and returns the results
268 sub render {
269 2 ▶ my $self = shift;
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
270 2 $self->as_sub->(@_);
271 }
272  
273 # Prints the output of this template with the passed params
274 sub run {
275 1 ▶ my $self = shift;
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
276 1 $self->as_print_sub->(@_);
277 }
278  
279 sub dump {
280 0 ▶ my $self = shift;
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
281 0 local $Data::Dumper::Deparse = 1;
282 0 return Data::Dumper->Dump( [$self], ['template'] );
283 }
284  
285 1 1;
« blib/lib/Templ/Tag.pm ↑ Summary Build »