lib/Any/Template.pm

filter: Statementstmt 53.8% 35 / 65 Branchbran 13.3% 4 / 30 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 69.2% 9 / 13 Podpod 66.6% 4 / 6 totaltotal 42.7% 53 / 124 CC 18 SCAR 33.6
CC
18
Cov
40%
_process
72.0
new
27.7
process
20.0
CRAP 28.8
↑ Summary blib/lib/Any/Template/Backend/Text/MicroMason.pm »
1 ###############################################################################
2 # Purpose : Consistent interface to templating modules
3 # Author : John Alden
4 # Created : Dec 2004
5 # CVS : $Header: /home/cvs/software/cvsroot/any_template/lib/Any/Template.pm,v 1.15 2006/04/18 08:32:26 johna Exp $
6 ###############################################################################
7  
8 package Any::Template;
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 $VERSION = sprintf"%d.%03d", q$Revision: 1.15 $ =~ /: (\d+)\.(\d+)/;
17  
18 1 ▶sub new {
Subroutine new: called CC=7 SCAR=33.2
Pod documented
19 1 my($class, $options) = @_;
20 1 DUMP("Any::Template constructor - options", $options);
21 1 ▶ $options->{Options} = {} unless defined ($options->{Options}); #Ensure this key is present
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"Options"} 1 0 1
22  
23 #Input checking
24 my $backend = $options->{Backend} || $ENV{ANY_TEMPLATE_DEFAULT}
25 1 ▶ or croak("You must nominate a Backend or set the ANY_TEMPLATE_DEFAULT environment variable");
Branch: 1 branch
BranchTrueFalseTotal
my $backend = $options->{"Backend"} || $ENV{"ANY_TEMPLATE_DEFAULT"} or croak("You must nominate a Backend or set the ANY_TEMPLATE_DEFAULT environment variable") 0 1 1
Condition33%1/3
$options->{'Backend'} || $ENV{'ANY_TEMPLATE_DEFAULT'}
l!l&&r!l&&!r
001
MC/DC0%0/2
$options->{'Backend'} || $ENV{'ANY_TEMPLATE_DEFAULT'}
$options->{'Backend'} $ENV{'ANY_TEMPLATE_DEFAULT'}
Truth table33%1/3
A || B
A= $options->{'Backend'}B= $ENV{'ANY_TEMPLATE_DEFAULT'}
ABresult
1X1
011
000
26 0 ▶ croak("Package name '$backend' looks incorrect") if($backend =~ /[^\w:]/);
Branch: 1 branch
BranchTrueFalseTotal
if $backend =~ /[^\w:]/ 0 0 0
27 0 my @sources = qw(Filename Filehandle String);
28 0 ▶ croak("You must supply one of: ".join(", ", @sources)) unless grep {defined $options->{$_}} @sources;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $options->{$_};} @sources 0 0 0
29
30 #Load backend
31 0 $backend = join("::", __PACKAGE__, 'Backend', $backend);
32 0 eval "require $backend";
33 0 ▶ die($@) if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
34  
35 #Create object (containing backend)
36 0 my $self = {};
37 0 $self->{backend} = $backend->new($options);
38 0 DUMP({Level => 2}, "Any::Template object", $self);
39 0 return bless($self, $class);
40 }
41  
42 0 ▶sub process {
Subroutine process: not called CC=4 SCAR=30.0
Pod documented
43 0 my($self, $data, $collector) = @_;
44
45 #Input checking
46 0 ▶ croak("You must supply a data structure") unless defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
47 0 ▶ croak("Data structure should be a reference") unless(ref $data);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
48
49 0 my $string;
50 0 ▶ if(defined $collector) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $collector) { } 0 0 0
51 0 $self->_process($data, $collector);
52 } else {
53 0 $self->_process($data, \$string);
54 }
55 0 return $string;
56 }
57  
58 0 ▶sub native_object {
Subroutine native_object: not called CC=1 SCAR=6.9
Pod documented
59 0 my $self = shift;
60 0 return $self->{backend}->native_object();
61 }
62  
63 #Find all the available backends for Any::Template
64 1 ▶sub available_backends {
Subroutine available_backends: called CC=1 SCAR=0.0
Pod documented
65 1 my @possible_locations = grep {-d $_} map {File::Spec->catfile($_, split(/::/, __PACKAGE__), "Backend")} @INC;
66
67 1 my @backends;
68 28 ▶ my $collector = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
69 28 ▶ return unless $_ =~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /\.pm$/ 12 16 28
70 16 my $file = $File::Find::name;
71 16 $file =~ s/\Q$File::Find::topdir\E//;
72 16 $file =~ s/\.pm$//;
73 16 my @dirs = File::Spec->splitdir($file);
74 16 shift @dirs;
75 16 $file = join("::", @dirs);
76 16 push @backends, $file;
77 1 };
78
79 1 File::Find::find($collector, @possible_locations);
80 1 return \@backends;
81 }
82  
83 #
84 # Private functions
85 #
86  
87 0 ▶sub _process {
Subroutine _process: not called CC=8 SCAR=42.8
88 0 my($self, $data, $collector) = @_;
89
90 #Preprocess data if required
91 0 $data = $self->{backend}->preprocess($data);
92  
93 #Type-based dispatch
94 0 ▶ die("Hash or array refs not supported as sinks") if(ref $collector eq "HASH" || ref $collector eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $collector eq "HASH" or ref $collector eq "ARRAY" 0 0 0
Condition0%0/3
ref $collector eq "HASH" or ref $collector eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $collector eq "HASH" or ref $collector eq "ARRAY"
ref $collector eq "HASH" ref $collector eq "ARRAY"
Truth table0%0/3
A or B
A= ref $collector eq "HASH"B= ref $collector eq "ARRAY"
ABresult
1X1
011
000
95 0 ▶ return $self->{backend}->process_to_string($data, $collector) if(ref $collector eq "SCALAR");
Branch: 1 branch
BranchTrueFalseTotal
if ref $collector eq "SCALAR" 0 0 0
96 0 ▶ return $self->{backend}->process_to_sub($data, $collector) if(ref $collector eq "CODE");
Branch: 1 branch
BranchTrueFalseTotal
if ref $collector eq "CODE" 0 0 0
97 0 ▶ return $self->{backend}->process_to_filehandle($data, $collector) if(ref $collector eq "GLOB"); #Filehandle ref
Branch: 1 branch
BranchTrueFalseTotal
if ref $collector eq "GLOB" 0 0 0
98 0 ▶ return $self->{backend}->process_to_filehandle($data, \$collector) if(ref \$collector eq "GLOB"); #Filehandle
Branch: 1 branch
BranchTrueFalseTotal
if ref \$collector eq "GLOB" 0 0 0
99 0 ▶ return $self->{backend}->process_to_file($data, $collector) if(not ref $collector); #Must come after check for glob
Branch: 1 branch
BranchTrueFalseTotal
unless ref $collector 0 0 0
100 0 return $self->{backend}->process_to_filehandle($data, $collector); #object - treat as a filehandle
101 }
102  
103 #Log::Trace stubs
104 0 ▶sub TRACE{}
Subroutine TRACE: not called CC=1 SCAR=0.0
Pod undocumented
105 1 ▶sub DUMP{}
Subroutine DUMP: called CC=1 SCAR=0.0
Pod undocumented
106  
107 =head1 NAME
108  
109 Any::Template - provide a consistent interface to a wide array of templating languages
110  
111 =head1 SYNOPSIS
112  
113 use Any::Template;
114 my $template = new Any::Template({
115 Backend => 'HTML::Template',
116 Filename => 'page.tmpl',
117 Options => {'strict' => 0}
118 });
119 my $output = $template->process($data);
120  
121 my $template2 = new Any::Template({
122 Backend => 'Text::Template',
123 String => $template2_content
124 });
125 $template->process($data, \*STDOUT);
126  
127 =head1 DESCRIPTION
128  
129 This module provides a simple, consistent interface to common templating engines so you can write code that
130 is agnostic to the template language used in the presentation layer. This means you can allow your interface
131 developers to work in the templating language they're happiest in or write code that works with legacy/in-house
132 templating modules but can also be released onto CPAN and work with more standard CPAN templating systems.
133  
134 By its very nature, this interface only exposes pretty much the lowest common denominator of the template engine APIs.
135 It does however provide a fairly rich set of input and output mechanisms, using native implementations where available
136 and providing some default implementations to extend the default set offered by some templating modules.
137  
138 If you need the quirky features of a particular templating engine, then this may not be for you.
139 Having said that, in some cases you may be able to encapsulate some of your logic in options passed into the adaptor classes
140 (either rolling your own adaptors, or improving ours) to pull the relevant strings on the backend module.
141  
142 Templateing languages supported by backends supplied with this distribution can be found in the README (remember there may be others out there and you can always roll your own).
143  
144 =head1 METHODS
145  
146 =over 4
147  
148 =item my $template = new Any::Template(\%options);
149  
150 See below for a list of options
151  
152 =item $template->process($data_structure, $sink);
153  
154 $sink can be:
155 - a scalar ref
156 - a filename (string)
157 - a filehandle (as a glob or glob ref) or an object offering a print method
158 - a coderef (output will be passed in as the first argument)
159  
160 =item $string = $template->process($data_structure);
161  
162 A convenience form, if no second argument is passed to C<process()>, equivalent to:
163
164 my $string;
165 $template->process($data_structure, \$string);
166  
167 except data is passed by value rather than by reference.
168  
169 =item $templating_engine = $template->native_object();
170  
171 Allows the native templating engine to be accessed.
172 This completely breaks the abstraction of Any::Template so it's not recommended you use it
173 other than as a bridging strategy as part of a refectoring/migration process
174 (with a view to ultimately eliminating its use).
175  
176 =back
177  
178 =head1 SUBROUTINES
179  
180 =over
181  
182 =item available_backends
183  
184 $list_of_backends = Any::Template::available_backends();
185  
186 Scans @INC for a list of modules in the Any::Template::Backend:: namespace.
187  
188 =back
189  
190 =head1 ERROR HANDLING
191  
192 If an error occurs, an exception is raised with die(). You can use an eval block to handle the exception. $@ will contain an error message.
193  
194 =head1 CONSTRUCTOR OPTIONS
195  
196 =over 4
197  
198 =item Backend
199  
200 Backends distributed with this module are listed in the distribution README.
201  
202 See L<Any::Template::Backend> for information on writing your own backends.
203 You should be able to create a new backend in a couple of dozen lines of code and
204 slot it into the unit test with a one or 2 line change.
205  
206 =item Filename
207  
208 Filename of the template file
209  
210 =item String
211  
212 String containing the template
213  
214 =item Filehandle
215  
216 Reference to a filehandle from which to read the template
217  
218 =item Options
219  
220 A hashref of options passed to the backend templating engine
221  
222 =back
223  
224 =head1 ENVIRONMENT
225  
226 If you don't supply a Backend to the constructor, Any::Template looks for a default Backend in the
227 ANY_TEMPLATE_DEFAULT environment variable. This allows you to retrofit Any::Template into legacy code without
228 hard-coding a default templating language or forcing a backwardly-incompatible change to the interface of the code you are retrofitting into.
229  
230 =head1 CACHING
231  
232 This module doesn't have built-in caching, however the objects it creates are intended to be cachable
233 (where possible the backends hold onto precompiled templates that can be fed any number of data structures).
234 It's therefore up to you what caching strategy you use. In the spirit of "if you liked this, you might also like..."
235 L<Cache> and L<Cache::Cache> offer a consistent interface to a number of different caching strategies.
236  
237 =head1 SEE ALSO
238  
239 =over 4
240  
241 =item *
242  
243 L<CGI::Application::Plugin::AnyTemplate>
244  
245 =item *
246  
247 L<Text::MicroMason>
248  
249 =item *
250  
251 L<Template>
252  
253 =item *
254  
255 L<HTML::Template>
256
257 =item *
258  
259 L<Text::Template>
260  
261 =back
262  
263 =head1 VERSION
264  
265 $Revision: 1.15 $ on $Date: 2006/04/18 08:32:26 $ by $Author: johna $
266  
267 =head1 AUTHOR
268  
269 John Alden <cpan _at_ bbc _dot_ co _dot_ uk>
270  
271 =head1 COPYRIGHT
272  
273 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
274  
275 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
276
277 =cut
↑ Summary blib/lib/Any/Template/Backend/Text/MicroMason.pm »