blib/lib/CGI/Template.pm

filter: Statementstmt 38.1% 66 / 173 Branchbran 20.2% 15 / 74 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 87.5% 7 / 8 totaltotal 35.4% 97 / 274 CC 44 SCAR 37.3
CC
44
Cov
33%
get_template
72.0
new
62.1
_check_template
46.4
CRAP 41.6
↑ Summary
1 package CGI::Template;
2  
3 1 ▶use 5.012004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9  
10  
11 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 $VERSION $FORM_CHECK @ISA @EXPORT @EXPORT_OK $DOCTYPE $STRICT
13 $TRANSITIONAL $FRAMESET $HTML5
14 1 );
15  
16  
17  
18 1 our @ISA = qw(Exporter);
19  
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
23  
24 # This allows declaration use CGI::Template ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
26 # will save memory.
27 1 our %EXPORT_TAGS = ( 'all' => [ qw(
28
29 ) ] );
30  
31 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
32  
33 1 our @EXPORT = qw();
34  
35 1 our $VERSION = '0.01';
36  
37  
38 # These variables just define standard HTML DOCTYPEs. We'll use them throughout.
39 1 $STRICT = qq{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n\n};
40 1 $TRANSITIONAL = qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n"http://www.w3.org/TR/html4/loose.dtd">\n\n};
41 1 $FRAMESET = qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"\n"http://www.w3.org/TR/html4/frameset.dtd">\n\n};
42 1 $HTML5 = qq{<!DOCTYPE html>};
43  
44 # Use HTML5 by default.
45 1 $DOCTYPE = $HTML5;
46  
47  
48  
49  
50 #
51 #
52 #
53 1 ▶sub new {
Subroutine new: called CC=17 SCAR=41.3
Pod documented
54 1 my $class = shift;
55  
56 1 my %passed_hash = @_;
57  
58 1 my $doctype = $passed_hash{doctype};
59 1 my $request_method = $passed_hash{request};
60 1 my $template_dir = $passed_hash{templates};
61  
62 1 ▶ $template_dir = "templates" unless $template_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $template_dir 1 0 1
63  
64 1 my $crm = $ENV{'REQUEST_METHOD'};
65  
66 1 ▶ if( $request_method ){
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method) 0 1 1
67 0 ▶ if( $request_method =~ /^get$/i ){
Branch: 2 branches
BranchTrueFalseTotal
if ($request_method =~ /^get$/iu) { } 0 0 0
elsif ($request_method =~ /^post$/iu) { } 0 0 0
68 0 ▶ unless( $crm =~ /^get$/i ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($crm =~ /^get$/iu) 0 0 0
69 0 &error("Incorrect request method");
70 }
71  
72 } elsif( $request_method =~ /^post$/i ){
73 0 ▶ unless( $crm =~ /^post$/i ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($crm =~ /^post$/iu) 0 0 0
74 0 &error("Incorrect request method");
75 }
76  
77 } else {
78 0 croak "CGI::Template : Invalid request argument passed to new(): $request_method";
79 }
80 }
81
82  
83 1 ▶ $doctype = "" unless $doctype;
Branch: 1 branch
BranchTrueFalseTotal
unless $doctype 1 0 1
84  
85 1 ▶ if( $doctype =~ /^transitional$/i || $doctype =~ /^loose$/i ){
Branch: 6 branches
BranchTrueFalseTotal
if ($doctype =~ /^transitional$/iu or $doctype =~ /^loose$/iu) { } 0 1 1
elsif ($doctype =~ /^frameset$/iu) { } 0 1 1
elsif ($doctype =~ /^strict$/iu) { } 0 1 1
elsif ($doctype =~ /^html5$/iu) { } 0 1 1
elsif ($doctype =~ /^none$/iu) { } 0 1 1
elsif ($doctype =~ /^$/iu) { } 1 0 1
Condition33%1/3
$doctype =~ /^transitional$/iu or $doctype =~ /^loose$/iu
l!l&&r!l&&!r
001
MC/DC0%0/2
$doctype =~ /^transitional$/iu or $doctype =~ /^loose$/iu
$doctype =~ /^transitional$/iu $doctype =~ /^loose$/iu
Truth table33%1/3
A or B
A= $doctype =~ /^transitional$/iuB= $doctype =~ /^loose$/iu
ABresult
1X1
011
000
86 0 $DOCTYPE = $TRANSITIONAL;
87  
88 } elsif( $doctype =~ /^frameset$/i ){
89 0 $DOCTYPE = $FRAMESET;
90  
91 } elsif( $doctype =~ /^strict$/i ){
92 0 $DOCTYPE = $STRICT;
93  
94 } elsif( $doctype =~ /^html5$/i ){
95 0 $DOCTYPE = $HTML5;
96  
97 } elsif( $doctype =~ /^none$/i ){
98 0 $DOCTYPE = "";
99  
100 } elsif( $doctype =~ /^$/i ){
101 1 $DOCTYPE = $HTML5;
102  
103 } else {
104 0 croak "CGI::Template : Invalid doctype argument passed to new(): $doctype";
105 }
106  
107 1 my $path = $ENV{PATH_INFO};
108 1 ▶ if( $path ){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 1 1
109 0 $path =~ s/^\/+//;
110 0 $path =~ s/\/+$//;
111 }
112  
113 1 my $self = {
114 path => $path,
115 templates => $template_dir,
116 };
117 1 my $template = &_check_template( $template_dir );
118 1 ▶ $self->{template} = $template if( $template );
Branch: 1 branch
BranchTrueFalseTotal
if $template 0 1 1
119  
120 1 bless($self, $class);
121 1 return $self;
122  
123 }
124  
125  
126  
127  
128 1 ▶sub header {
Subroutine header: called CC=6 SCAR=26.0
Pod documented
129 1 my $self = shift;
130 1 my %passed_hash = @_;
131  
132 1 my $header = "Content-type: text/html\n";
133  
134 1 my $redirect = 0;
135 1 foreach my $i (keys %passed_hash){
136 0 ▶ if( $i =~ /-cookie/ ){
Branch: 3 branches
BranchTrueFalseTotal
if ($i =~ /-cookie/u) { } 0 0 0
elsif ($i =~ /-content-type/u) { } 0 0 0
elsif ($i =~ /-redirect/u) { } 0 0 0
137 0 $header .= "Set-Cookie: " . $passed_hash{$i} . "\n";
138  
139 } elsif( $i =~ /-content-type/ ) {
140 0 $header =~ s/text\/html/$passed_hash{$i}/;
141  
142 } elsif( $i =~ /-redirect/ ) {
143 0 $header =~ s/Content-type: .*?\n/Status: 302 Found\nLocation: $passed_hash{$i}\n/m;
144 0 $redirect++;
145  
146 } else {
147 0 $header .= "$i: " . $passed_hash{$i} . "\n";
148 }
149 }
150  
151 1 $header .= "\n";
152 1 ▶ $header .= $DOCTYPE unless $redirect;
Branch: 1 branch
BranchTrueFalseTotal
unless $redirect 1 0 1
153  
154 1 return $header;
155 }
156  
157 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159 0 return $self->{path};
160 }
161  
162  
163 0 ▶sub content {
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
164 0 my $self = shift;
165 0 my %hash = @_;
166  
167 0 ▶ croak "CGI::Template : No template set!" unless defined $self->{template};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"template"} 0 0 0
168  
169 0 my $data = $self->{template};
170  
171 0 foreach my $i (keys %hash){
172 0 $data =~ s/#!$i!#/$hash{$i}/g;
173 }
174  
175 0 return $data;
176 }
177  
178 0 ▶sub replace_template {
Subroutine replace_template: not called CC=1 SCAR=6.9
Pod documented
179 0 my $self = shift;
180 0 my $template = shift;
181 0 $self->{template} = $template;
182 }
183  
184 0 ▶sub error {
Subroutine error: not called CC=5 SCAR=34.0
Pod documented
185 0 my $self = shift;
186 0 my $message = shift;
187  
188 0 my $template_dir = $self->{templates};
189  
190 0 my $error_template = '';
191 0 ▶ if( -e "$template_dir/error.html" ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$template_dir/error.html") 0 0 0
192 0 my $fail = 0;
193 0 ▶ open INFILE, "$template_dir/error.html" or $fail = 1;
Branch: 1 branch
BranchTrueFalseTotal
open INFILE, "$template_dir/error.html" or $fail = 1 0 0 0
194 0 ▶ if( $fail ) { croak "CGI::Template : Couldn't open error template: $template_dir/error.html"; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 0 0 0
195 {
196 0 local $/;
197 0 $error_template = <INFILE>;
198 }
199 0 close INFILE;
200 }
201  
202 0 ▶ $error_template = qq{
Branch: 1 branch
BranchTrueFalseTotal
unless $error_template 0 0 0
203 <html>
204 <head><title>Error</title></head>
205 <body>
206 <h1>Oops!</h1>
207 <p>#!MESSAGE!#</p>
208 <p></p>
209 <hr>
210 </body>
211 </html>
212 } unless( $error_template );
213  
214 0 $error_template =~ s/#!MESSAGE!#/$message/g;
215 0 print &header;
216 0 print $error_template;
217 0 exit;
218  
219 }
220  
221  
222 0 ▶sub get_template {
Subroutine get_template: not called CC=8 SCAR=42.8
Pod documented
223 0 my $self = shift;
224 0 my $file = shift;
225 0 my %passed_hash = @_;
226 0 my $fail = 0;
227  
228 0 my $template_dir = $self->{templates};
229  
230 0 my @templates = (
231 "$template_dir/$file.html",
232 "$template_dir/$file",
233 );
234  
235 0 my $tn = '';
236 0 foreach my $i (@templates){
237 0 ▶ if( -e $i ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $i) 0 0 0
238 0 $tn = $i;
239 0 last;
240 }
241 }
242  
243 0 ▶ unless($tn){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tn) 0 0 0
244 0 carp "CGI::Template : Couldn't find valid template: ".join(", ", @templates)." called by $tn";
245 0 ▶ return (0) unless $tn;
Branch: 1 branch
BranchTrueFalseTotal
unless $tn 0 0 0
246 }
247  
248  
249 0 ▶ open INFILE, $tn or $fail = 1;
Branch: 1 branch
BranchTrueFalseTotal
open INFILE, $tn or $fail = 1 0 0 0
250 0 ▶ if( $fail ) { carp "CGI::Template : Couldn't open template: $file, called by $tn"; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 0 0 0
251  
252 0 my $data = '';
253 {
254 0 local $/;
255 0 $data = <INFILE>;
256 }
257 0 close INFILE;
258  
259 0 foreach my $i (keys %passed_hash){
260 0 $i = uc($i);
261 0 $data =~ s/#!$i!#/$passed_hash{$i}/g;
262 }
263  
264 0 return $data;
265  
266  
267 }
268  
269 0 ▶sub template {
Subroutine template: not called CC=1 SCAR=6.9
Pod undocumented
270 0 my $self = shift;
271 0 return $self->{template};
272 }
273  
274  
275  
276 ###########################################################
277 # PRIVATE METHODS
278 ###########################################################
279  
280 1 ▶sub _check_template {
Subroutine _check_template: called CC=10 SCAR=38.4
281 1 my $template_dir = shift;
282  
283 1 my $name = $0;
284
285 1 $name =~ s/\/$//;
286 1 $name =~ s/^.*\///;
287 1 $name =~ s/\.cgi$//;
288 1 $name =~ s/\.pl$//;
289  
290 1 my @templates = (
291 "$template_dir/$name.html",
292 "$template_dir/$name",
293 "$template_dir/default.html"
294 );
295  
296  
297 1 my $head_template = '';
298 1 ▶ if( -e "$template_dir/head.html" ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$template_dir/head.html") 0 1 1
299 0 my $fail = 0;
300 0 ▶ open INFILE, "$template_dir/head.html" or $fail = 1;
Branch: 1 branch
BranchTrueFalseTotal
open INFILE, "$template_dir/head.html" or $fail = 1 0 0 0
301 0 ▶ if( $fail ) { croak "CGI::Template : Couldn't open general template: $template_dir/head.html, called by $name"; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 0 0 0
302 {
303 0 local $/;
304 0 $head_template = <INFILE>;
305 }
306 0 close INFILE;
307 }
308  
309 1 my $tn = '';
310 1 foreach my $i (@templates){
311 3 ▶ if( -e $i ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $i) 0 3 3
312 0 $tn = $i;
313 0 last;
314 }
315 }
316  
317 1 ▶ return (0) unless $tn;
Branch: 1 branch
BranchTrueFalseTotal
unless $tn 1 0 1
318  
319 0 my $fail = 0;
320 0 ▶ open INFILE, $tn or $fail = 1;
Branch: 1 branch
BranchTrueFalseTotal
open INFILE, $tn or $fail = 1 0 0 0
321  
322 0 ▶ if( $fail ) { carp "CGI::Template : Couldn't open template: $tn, called by $name"; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 0 0 0
323  
324 0 my $data = '';
325 {
326 0 local $/;
327 0 $data = <INFILE>;
328 }
329 0 close INFILE;
330  
331 0 my $template = "";
332  
333 0 ▶ if( $head_template ){
Branch: 1 branch
BranchTrueFalseTotal
if ($head_template) { } 0 0 0
334 0 $template = $head_template;
335 0 $template =~ s/#!DATA!#/$data/;
336 } else {
337 0 $template = $data;
338 }
339  
340 0 return $template;
341  
342 }
343  
344  
345  
346  
347  
348  
349  
350 1 1;
351 __END__
↑ Summary