Templ/Template.pm

filter: Statementstmt 86.6% 156 / 180 Branchbran 54.1% 26 / 48 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 36 / 40 Podpod 0.0% 0 / 18 totaltotal 71.9% 223 / 310 CC 33 SCAR 14.0
CC
33
Cov
77%
resolve_source
11.5
new
6.9
as_pretty_perl
6.0
CRAP 4.0
« Templ/Parser.pm ↑ Summary Templ/Tag.pm »
1 package Templ::Template;
2  
3 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 push @ISA, 'Exporter';
5 1 @EXPORT = qw(add_header add_tag);
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw(blessed openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Templ::Parser::Print;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Templ::Parser::Return;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Templ::Util qw(expand_isa);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use overload '""' => \&as_perl, '&{}' => \&as_sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 my %templ_cache = ();
22  
23 ##############################################################################
24 # Subclass import functions
25  
26 1 ▶sub add_header ($) {
Subroutine add_header: called CC=1 SCAR=0.0
Pod undocumented
27  
28 1 my $target_class = caller; # The class name of the template we are
29 # adding a header to
30 1 my $header = shift; # Header to associate with the template
31  
32 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 push @{ $target_class . '::TEMPL_HEADERS' }, $header;
35  
36 }
37  
38 6 ▶sub add_tag ($$;%) {
Subroutine add_tag: called CC=3 SCAR=11.6
Pod undocumented
39  
40 6 my $target_class = caller; # The class name of the template we are
41 # adding a tag to
42 6 my $char = shift; # Character to associate with the tag
43 6 my $tag_class = shift; # The tag's class name we're adding to
44 # the calling template
45  
46 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 eval "
48 require Templ::Tag::$tag_class;
49 require $tag_class;
50 ";
51 6 ▶ if ( scalar keys %{ 'Templ::Tag::' . $tag_class . '::' } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %{'Templ::Tag::' . $tag_class . '::';}) { } 6 0 6
elsif (not scalar keys %{$tag_class . '::';}) { } 0 0 0
52 6 $tag_class = 'Templ::Tag::' . $tag_class;
53 }
54 0 elsif ( not scalar keys %{ $tag_class . '::' } ) {
55 0 croak "Unable to resolve Templ::Tag class $tag_class";
56 }
57  
58 6 push @{ $target_class . '::TEMPL_TAGS' },
59 $tag_class->new( 'char' => $char, @_ );
60  
61 }
62  
63 ##############################################################################
64 # Class methods
65  
66 4 ▶sub new {
Subroutine new: called CC=6 SCAR=19.4
Pod undocumented
67  
68 4 my $class = shift; # Might be a partial class if called from get()
69 4 ▶ if ( not defined $class || ref $class || $class !~ m/^(\w+\:\:)*\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/) 0 4 4
Condition33%2/6
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
l!l&&r!l&&!r
400
defined $class or ref $class
l!l&&r!l&&!r
400
MC/DC0%0/3
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
defined $class ref $class not $class =~ /^(\w+\:\:)*\w+$/
Truth table25%1/4
A or B or not(C)
A= defined $classB= ref $classC= not $class =~ /^(\w+\:\:)*\w+$/
ABCresult
1XX1
01X1
0011
0000
70 0 croak "Can only be called as __PACKAGE__->new";
71 }
72 4 eval "require Templ::Template::$class; require $class;";
73 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶ if ( scalar keys %{ 'Templ::Template::' . $class . '::' } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %{'Templ::Template::' . $class . '::';}) { } 3 1 4
elsif (not scalar keys %{$class . '::';}) { } 0 1 1
75 3 $class = 'Templ::Template::' . $class;
76 }
77 1 elsif ( not scalar keys %{ $class . '::' } ) {
78 0 croak "Unable to resolve Templ::Template class $class";
79 }
80  
81 4 my $self = bless {}, $class;
82 4 $self->resolve_source( @_ );
83  
84 4 return $self;
85 }
86  
87 # Creates a new Templ::Template* object of the passed type
88 3 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
89 3 my $class = shift;
90 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 my $id = md5( join "\n", ( caller(1), @_ ) );
92 3 ▶ if ( not defined $templ_cache{$id} ) { $templ_cache{$id} = new( @_ ); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $templ_cache{$id}) 3 0 3
93 3 return $templ_cache{$id};
94 }
95  
96 ##############################################################################
97 # Hybrid class/object methods
98  
99 4 ▶sub tags {
Subroutine tags: called CC=5 SCAR=16.3
Pod undocumented
100 4 ▶ my $class = ref( $_[0] ) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
400
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
101 4 my @tags = ();
102 4 foreach my $this_class ( expand_isa($class) ) {
103 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 15 ▶ next unless scalar( @{ $this_class . '::TEMPL_TAGS' } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$this_class . '::TEMPL_TAGS';} 8 7 15
105 7 push @tags, @{ $this_class . '::TEMPL_TAGS' };
106 }
107 4 ▶ return wantarray ? @tags : \@tags;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
108 }
109  
110 4 ▶sub header {
Subroutine header: called CC=4 SCAR=13.9
Pod undocumented
111 4 ▶ my $class = ref( $_[0] ) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
400
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
112 4 my @headers = ();
113 4 foreach my $this_class ( expand_isa($class) ) {
114 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 15 ▶ next unless scalar( @{ $this_class . '::TEMPL_HEADERS' } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$this_class . '::TEMPL_HEADERS';} 12 3 15
116 3 push @headers, @{ $this_class . '::TEMPL_HEADERS' };
117 }
118 4 return join '', map {"$_\n"} @headers;
119 }
120  
121 ##############################################################################
122 # Object methods
123  
124 # Get the template contents of the object
125 4 ▶sub templ_code {
Subroutine templ_code: called CC=1 SCAR=0.0
Pod undocumented
126 4 my $self = shift;
127 4 return $self->{'templ_code'};
128 }
129  
130 4 ▶sub resolve_source {
Subroutine resolve_source: called CC=9 SCAR=24.4
Pod undocumented
131
132 4 my $self = shift;
133 4 my $source = shift;
134
135 4 ▶ return unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 4 4
136
137 4 $self->clear;
138  
139 4 ▶ if (not ref $source) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $source) { } 2 2 4
elsif (ref $source eq 'ARRAY') { } 0 2 2
elsif (ref $source eq 'SCALAR') { } 1 1 2
elsif (&openhandle($source) or eval { do { $source->can('getline') } }) { } 1 0 1
140 2 $self->{'templ_code'} = $source;
141 }
142 elsif (ref($source) eq 'ARRAY') {
143 0 $self->{'templ_code'} = join '', @{$source};
144 }
145 elsif ( ref($source) eq 'SCALAR' ) {
146 1 ▶ croak "Unable to stat file '$source'" unless -f $$source;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $$source 0 1 1
147 1 my $fh = IO::File->new($$source, 'r');
148 1 ▶ defined($fh) || croak "Unable to open file ".$$source.": $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or croak('Unable to open file ' . $$source . ": $!") 1 0 1
149 1 $self->{'templ_code'} = join '', $fh->getlines;
150 1 $fh->close;
151 }
152 0 ▶ elsif ( openhandle($source) || eval { $source->can('getline') }) {
Condition33%1/3
&openhandle($source) or eval { do { $source->can("getline") } }
l!l&&r!l&&!r
100
MC/DC0%0/2
&openhandle($source) or eval { do { $source->can("getline") } }
&openhandle($source) eval { do { $source->can("getline") } }
Truth table33%1/3
A or B
A= &openhandle($source)B= eval { do { $source->can("getline") } }
ABresult
1X1
011
000
153 1 local $/ = undef;
154 1 $self->{'templ_code'} = <$source>;
155 1 close $source;
156 }
157 else {
158 0 croak "Unrecognized Templ source parameter: ".Dumper($source);
159 }
160 }
161  
162 # Returns an eval-able string perl block which returns the output of the
163 # template
164 1 ▶sub as_perl {
Subroutine as_perl: called CC=2 SCAR=7.0
Pod undocumented
165 1 my $self = shift;
166 1 ▶ if ( not defined $self->{'as_perl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_perl"}) 1 0 1
167 1 $self->{'as_perl'} = '{'
168 . Templ::Parser::Return->new()->parse($self)
169 . '}';
170 }
171 1 return $self->{'as_perl'};
172 }
173  
174 # Returns an eval-able string perl block which returns the output of the
175 # template, with newline-spanning strings split into multiple perl code lines
176 0 ▶sub as_pretty_perl {
Subroutine as_pretty_perl: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my $self = shift;
178 0 ▶ if ( not defined $self->{'as_pretty_perl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_pretty_perl"}) 0 0 0
179 0 $self->{'as_pretty_perl'} = '{'
180 . Templ::Parser::Return->new( 'prettyify' => 1 )->parse($self)
181 . '}';
182 }
183 0 return $self->{'as_pretty_perl'};
184 }
185  
186 # Returns a code reference to a block-based handler for the template
187 3 ▶sub as_sub {
Subroutine as_sub: called CC=3 SCAR=11.0
Pod undocumented
188 3 my $self = shift;
189 3 ▶ if ( not defined $self->{'as_sub'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_sub"}) 2 1 3
190 2 my $sub;
191 1 ▶ eval '$sub = sub {' . Templ::Parser::Return->new()->parse($self) . '}';
Subroutine BEGIN: called CC=1 SCAR=0.0
192 2 ▶ $@ && croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($@) 0 2 2
193 2 $self->{'as_sub'} = $sub;
194 }
195 3 return $self->{'as_sub'};
196 }
197  
198 # Runs the return handler on the passed params... in other words, executes
199 # the template and returns the results
200 3 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
201 3 my $self = shift;
202 3 $self->as_sub->(@_);
203 }
204  
205 # Runs the print handler on the passed params... in other words it executes
206 # the template in such a way that the output is sent to the select()ed FH
207 0 ▶sub as_print {
Subroutine as_print: not called CC=2 SCAR=17.9
Pod undocumented
208 0 my $self = shift;
209 0 ▶ if ( not defined $self->{'as_print'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_print"}) 0 0 0
210 0 $self->{'as_print'} = '{'
211 . Templ::Parser::Print->new()->parse($self)
212 . '}';
213 }
214 0 return $self->{'as_print'};
215 }
216  
217 # Returns an eval-able string perl block which returns the output of the
218 # template, with newline-spanning strings split into multiple lines
219 0 ▶sub as_pretty_print {
Subroutine as_pretty_print: not called CC=2 SCAR=17.9
Pod undocumented
220 0 my $self = shift;
221 0 ▶ if ( not defined $self->{'as_pretty_print'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_pretty_print"}) 0 0 0
222 0 $self->{'as_pretty_print'} = '{'
223 . Templ::Parser::Print->new( 'prettyify' => 1 )->parse($self)
224 . '}';
225 }
226 0 return $self->{'as_pretty_print'};
227 }
228  
229 # Returns a code reference to a printing handler for this template
230 1 ▶sub as_print_sub {
Subroutine as_print_sub: called CC=3 SCAR=11.1
Pod undocumented
231 1 my $self = shift;
232 1 ▶ if ( not defined $self->{'as_print_sub'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"as_print_sub"}) 1 0 1
233 1 my $sub;
234 1 ▶ eval '$sub = sub {' . Templ::Parser::Print->new()->parse($self) . '}';
Subroutine BEGIN: called CC=1 SCAR=0.0
235 1 ▶ $@ && croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($@) 0 1 1
236 1 $self->{'as_print_sub'} = $sub;
237 }
238 1 return $self->{'as_print_sub'};
239 }
240  
241 # Prints the output of this template with the passed params
242 1 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
243 1 my $self = shift;
244 1 $self->as_print_sub->(@_);
245 }
246  
247 4 ▶sub clear {
Subroutine clear: called CC=2 SCAR=7.2
Pod undocumented
248 4 my $self = shift;
249 0 delete $self->{$_} foreach grep { m/^as_/ } keys %{$self};
250 }
251  
252 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
253 0 my $self = shift;
254 0 local $Data::Dumper::Deparse = 1;
255 0 return Data::Dumper->Dump( [$self], ['template'] );
256 }
257  
258 1 1;
« Templ/Parser.pm ↑ Summary Templ/Tag.pm »