CGI/Portable/AppStatic.pm

filter: Statementstmt 29.2% 19 / 65 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 4 / 10 Podpod 100% 6 / 6 totaltotal 25.0% 29 / 116 CC 24 SCAR 36.7
CC
24
Cov
19%
set_static_low_replace
56.0
set_static_high_replace
56.0
set_static_attach_ordered
56.0
CRAP 39.3
« DemoTextFile.pm ↑ Summary DemoGuestBook.pm »
1 =head1 NAME
2  
3 CGI::Portable::AppStatic - Define whole response screens within a config file
4  
5 =cut
6  
7 ######################################################################
8  
9 package CGI::Portable::AppStatic;
10 1 require 5.004;
11  
12 # Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module
13 # is free software; you can redistribute it and/or modify it under the same terms
14 # as Perl itself. However, I do request that this copyright information and
15 # credits remain attached to the file. If you modify this module and
16 # redistribute a changed version then please attach a note listing the
17 # modifications. This module is available "as-is" and the author can not be held
18 # accountable for any problems resulting from its use.
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = '0.50';
24  
25 ######################################################################
26  
27 =head1 DEPENDENCIES
28  
29 =head2 Perl Version
30  
31 5.004
32  
33 =head2 Standard Modules
34  
35 I<none>
36  
37 =head2 Nonstandard Modules
38  
39 CGI::Portable 0.50
40  
41 =cut
42  
43 ######################################################################
44  
45 1 ▶use CGI::Portable 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 ######################################################################
48  
49 =head1 SYNOPSIS
50  
51 =head2 Simple program that returns a static HTML page:
52  
53 #!/usr/bin/perl
54 use strict;
55 use warnings;
56  
57 require CGI::Portable;
58 my $globals = CGI::Portable->new();
59  
60 my %CONFIG = (
61 high_http_status_code => '200 OK',
62 high_http_content_type => 'text/html',
63 high_page_title => 'Simple AppStatic Demo',
64 high_page_author => 'Darren Duncan',
65 high_page_meta => {
66 keywords => 'HTTP, HTML, Perl, Static',
67 },
68 high_page_style_code => [
69 'body {background-color: white; background-image: none}',
70 'h1, h2 {text-align: center}',
71 'td {text-align: left; vertical-align: top}',
72 ],
73 high_page_body => __endquote,
74 <h1>Simple AppStatic Demo</h1>
75 <p>This page is a trivial example of what can be done with CGI::Portable
76 when you want your script to always return the same screen. It is more
77 common, however, that your script would contain many screens of which some
78 are dynamic and some are static.</p>
79 <h2>Oh, A Table!</h2>
80 <table><tr>
81 <td>Question:</td><td>Answer!</td>
82 </tr><tr>
83 <td>Another Question:</td>
84 <td>This is a really really long answer. It just keeps going on and on and
85 on and on and on and on and on and on and on. However, the short question
86 should stay top aligned with the long answer due to the stylesheet.</td>
87 </tr></table>
88 __endquote
89 );
90  
91 $globals->set_prefs( \%CONFIG );
92 $globals->call_component( 'CGI::Portable::AppStatic' );
93  
94 require CGI::Portable::AdapterCGI;
95 my $io = CGI::Portable::AdapterCGI->new();
96 $io->send_user_output( $globals );
97  
98 1;
99  
100 =head2 Simple program that returns an HTTP redirection header:
101  
102 my %CONFIG = (
103 high_http_status_code => '301 Moved',
104 high_http_window_target => 'nyx_demo_file_window',
105 high_http_redirect_url => 'http://www.nyxmydomain.net/dir/file.html',
106 );
107  
108 =head2 Simple program that returns a bit of binary data:
109  
110 my %CONFIG = (
111 high_http_status_code => '200 OK',
112 high_http_content_type => 'application/xxxencoded',
113 high_http_body => pack( 'H8', '5065726c' ),
114 high_http_body_is_binary => 1,
115 );
116  
117 =head1 DESCRIPTION
118  
119 This Perl 5 object class is a simple encapsulated application, or "component",
120 that runs in the CGI::Portable environment. It allows you to define a complete
121 static "program response screen" within the standard "preferences" config file
122 without having to write your own "application" to do it. Or, to be specific,
123 this module allows you to set any CGI::Portable 'Response' properties by
124 providing a like-named "preference" with each new value, rather than having to
125 explicitely call each appropriate accessor method. This module is designed to be
126 easily subclassed by your own application components, so they can do the same
127 things while you only need to program the interesting dynamic functionality.
128 An example scenario has users of your subclassed application using AppStatic
129 methods to apply a common header or footer or stylesheet to every screen.
130  
131 =cut
132  
133 ######################################################################
134  
135 # Constant values used by this class:
136  
137 # root property/preference names applied as "low/high" scalars:
138 1 my @SCALAR_PREFS = qw(
139 http_status_code http_window_target http_content_type
140 http_redirect_url http_body http_body_is_binary
141 page_prologue page_title page_author
142 );
143  
144 # root property/preference names applied as "low/high" list refs:
145 1 my @PREFS_TO_SET = qw(
146 http_cookies http_headers page_meta page_style_sources
147 page_style_code page_head page_frameset_attributes
148 page_frameset page_body_attributes page_body
149 );
150  
151 # root property/preference names applied as "add" list refs:
152 1 my @PREFS_TO_ADD = qw(
153 http_cookies http_headers page_meta page_style_sources page_style_code
154 page_frameset_attributes page_body_attributes
155 );
156  
157 # root property/preference names applied as "append/prepend" list refs:
158 1 my @PREFS_TO_PEND = qw(
159 page_head page_frameset page_body
160 );
161  
162 ######################################################################
163  
164 =head1 SYNTAX
165  
166 This class does not export any functions or methods, so you need to call them
167 using object notation. This means using B<Class-E<gt>function()> for functions
168 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
169 your own modules, then that often means something like B<$self-E<gt>method()>.
170  
171 This class does not have any of its own stored properties, so there is no risk
172 of property namespace collisions with subclasses. Instead, you must provide the
173 CGI::Portable object to each method explicitely as its first argument.
174  
175 =head1 THE ONLY PUBLIC METHOD
176  
177 =head2 main( GLOBALS )
178  
179 You invoke this method to run the application component that is encapsulated by
180 this class. The required argument GLOBALS is an CGI::Portable object that
181 you have previously configured to hold the instance settings and user input for
182 this class. When this method returns then the encapsulated application will
183 have finished and you can get its user output from the CGI::Portable object.
184  
185 This method is simple and intended to be overriden by subclasses. All it does
186 by itself is invoke the private methods listed under PRIVATE METHODS FOR USE BY
187 SUBCLASSES, which do the actual work.
188  
189 =cut
190  
191 ######################################################################
192  
193 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
Pod documented
194 0 my ($class, $globals) = @_;
195 0 ▶ my $self = bless( {}, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
196  
197 0 ▶ UNIVERSAL::isa( $globals, 'CGI::Portable' ) or
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($globals, "CGI::Portable") or die "initializer is not a valid CGI::Portable object" 0 0 0
198 die "initializer is not a valid CGI::Portable object";
199  
200 0 $self->set_static_low_replace( $globals );
201 0 $self->set_static_high_replace( $globals );
202 0 $self->set_static_attach_unordered( $globals );
203 0 $self->set_static_attach_ordered( $globals );
204 0 $self->set_static_search_and_replace( $globals );
205 }
206  
207 ######################################################################
208  
209 =head1 PREFERENCES HANDLED BY THIS MODULE
210  
211 These preferences have names that are the same as the CGI::Portable 'Response'
212 properties that their values are being used to set, except that each has a prefix
213 indicating how the value is set. The prefixes used include any of the following,
214 where they make sense for each property: "low", "high", "add", "append",
215 "prepend". Preferences are used only if they have a defined value.
216  
217 The first two prefixes are valid with all scalar and list properties; "low"
218 priority values are applied as early as possible, and will be lost if the
219 subclassed application decides to set the same properties; "high" priority values
220 are assigned after the "main program" does its work, and will overwrite any same
221 properties that the "main program" sets.
222  
223 The latter three prefixes only make sense with non-scalar or "list" properties
224 where each property can store multiple values; they are all applied as late as
225 possible, and try to preserve existing property values while adding their own.
226 The "append" and "prepend" prefixes are only used where the order of the property
227 elements is important, such as in the "page body". The "add" prefix is only used
228 where the order is not preserved, such as with the "page meta" tags.
229  
230 The page_search_and_replace preference is different, having no prefixes, and is
231 applied after all of the other preferences.
232  
233 This module assumes that the CGI::Portable object passed to it did not have any
234 of its output properties set prior to main() being called, so even the "low"
235 priority preferences will overwrite any pre-existing same properties. To avoid
236 unpleasantries it is a good idea for calling modules to apply all of their own
237 output after called modules have finished, or to employ make_new_context() and
238 related functionality.
239  
240 =head2 These preferences are for generic HTTP responses:
241  
242 low_http_status_code - string
243 low_http_window_target - string
244 low_http_content_type - string
245 low_http_redirect_url - string
246 low_http_cookies - array of encoded cookie strings
247 low_http_headers - hash of header names and values
248 low_http_body - string
249 low_http_body_is_binary - boolean
250  
251 high_http_status_code - string
252 high_http_window_target - string
253 high_http_content_type - string
254 high_http_redirect_url - string
255 high_http_cookies - array of encoded cookie strings
256 high_http_headers - hash of header names and values
257 high_http_body - string
258 high_http_body_is_binary - boolean
259  
260 add_http_cookies - array of encoded cookie strings
261 add_http_headers - hash of header names and values
262  
263 =head2 These preferences are specifically for HTML pages:
264  
265 low_page_prologue - string (override the DOCTYPE tag)
266 low_page_title - string
267 low_page_author - string
268 low_page_meta - hash of meta-tag names and values
269 low_page_style_sources - array of strings (urls)
270 low_page_style_code - array of strings (raw code)
271 low_page_head - array
272 low_page_frameset_attributes - hash
273 low_page_frameset - array of hashes (each hash is attributes for new FRAME tag)
274 low_page_body_attributes - hash
275 low_page_body - array
276  
277 high_page_prologue - string (override the DOCTYPE tag)
278 high_page_title - string
279 high_page_author - string
280 high_page_meta - hash of meta-tag names and values
281 high_page_style_sources - array of strings (urls)
282 high_page_style_code - array of strings (raw code)
283 high_page_head - array
284 high_page_frameset_attributes - hash
285 high_page_frameset - array of hashes (each hash is attributes for new FRAME tag)
286 high_page_body_attributes - hash
287 high_page_body - array
288  
289 add_page_meta - hash of meta-tag names and values
290 add_page_style_sources - array of strings (urls)
291 add_page_style_code - array of strings (raw code)
292 add_page_frameset_attributes - hash
293 add_page_body_attributes - hash
294  
295 prepend_page_head - array
296 prepend_page_frameset - array of hashes (each hash is attributes for new FRAME tag)
297 prepend_page_body - array
298  
299 append_page_head - array
300 append_page_frameset - array of hashes (each hash is attributes for new FRAME tag)
301 append_page_body - array
302  
303 page_search_and_replace - hash (keys are tokens to search for; values replace)
304  
305 =head1 PRIVATE METHODS FOR USE BY SUBCLASSES
306  
307 =head2 set_static_low_replace( GLOBALS )
308  
309 This method will apply all of the "low" priority preferences, which replace
310 any respective properties.
311  
312 =cut
313  
314 ######################################################################
315  
316 0 ▶sub set_static_low_replace {
Subroutine set_static_low_replace: not called CC=7 SCAR=40.3
Pod documented
317 0 my ($self, $globals) = @_;
318 0 my $rh_prefs = $globals->get_prefs_ref();
319 0 foreach my $base (@SCALAR_PREFS) {
320 0 ▶ if( defined( $rh_prefs->{"low_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"low_$base"}) 0 0 0
321 0 eval "\$globals->$base( \$rh_prefs->{low_$base} );";
322 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
323 }
324 }
325 0 foreach my $base (@PREFS_TO_SET) {
326 0 ▶ if( defined( $rh_prefs->{"low_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"low_$base"}) 0 0 0
327 0 eval "\$globals->set_$base( \$rh_prefs->{low_$base} );";
328 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
329 }
330 }
331 }
332  
333 ######################################################################
334  
335 =head2 set_static_high_replace( GLOBALS )
336  
337 This method will apply all of the "high" priority preferences, which replace
338 any respective properties.
339  
340 =cut
341  
342 ######################################################################
343  
344 0 ▶sub set_static_high_replace {
Subroutine set_static_high_replace: not called CC=7 SCAR=40.3
Pod documented
345 0 my ($self, $globals) = @_;
346 0 my $rh_prefs = $globals->get_prefs_ref();
347 0 foreach my $base (@SCALAR_PREFS) {
348 0 ▶ if( defined( $rh_prefs->{"high_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"high_$base"}) 0 0 0
349 0 eval "\$globals->$base( \$rh_prefs->{high_$base} );";
350 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
351 }
352 }
353 0 foreach my $base (@PREFS_TO_SET) {
354 0 ▶ if( defined( $rh_prefs->{"high_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"high_$base"}) 0 0 0
355 0 eval "\$globals->set_$base( \$rh_prefs->{high_$base} );";
356 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
357 }
358 }
359 }
360  
361 ######################################################################
362  
363 =head2 set_static_attach_unordered( GLOBALS )
364  
365 This method will apply all of the "add" preferences, which try to add values to
366 any respective properties without deleting previous values. Previous values
367 are only deleted where the properties are hashes and new hash keys are the same
368 as existing ones; different keys do not conflict.
369  
370 =cut
371  
372 ######################################################################
373  
374 0 ▶sub set_static_attach_unordered {
Subroutine set_static_attach_unordered: not called CC=4 SCAR=30.0
Pod documented
375 0 my ($self, $globals) = @_;
376 0 my $rh_prefs = $globals->get_prefs_ref();
377 0 foreach my $base (@PREFS_TO_ADD) {
378 0 ▶ if( defined( $rh_prefs->{"add_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"add_$base"}) 0 0 0
379 0 eval "\$globals->add_$base( \$rh_prefs->{add_$base} );";
380 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
381 }
382 }
383 }
384  
385 ######################################################################
386  
387 =head2 set_static_attach_ordered( GLOBALS )
388  
389 This method will apply all of the "append" and "prepend" preferences, which will
390 always add to their respective properties without deleting previous values.
391  
392 =cut
393  
394 ######################################################################
395  
396 0 ▶sub set_static_attach_ordered {
Subroutine set_static_attach_ordered: not called CC=7 SCAR=40.3
Pod documented
397 0 my ($self, $globals) = @_;
398 0 my $rh_prefs = $globals->get_prefs_ref();
399 0 foreach my $base (@PREFS_TO_PEND) {
400 0 ▶ if( defined( $rh_prefs->{"append_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"append_$base"}) 0 0 0
401 0 eval "\$globals->append_$base( \$rh_prefs->{append_$base} );";
402 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
403 }
404 }
405 0 foreach my $base (@PREFS_TO_PEND) {
406 0 ▶ if( defined( $rh_prefs->{"prepend_$base"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rh_prefs{"prepend_$base"}) 0 0 0
407 0 eval "\$globals->prepend_$base( \$rh_prefs->{prepend_$base} );";
408 0 ▶ $@ and die;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 0 0
409 }
410 }
411 }
412  
413 ######################################################################
414  
415 =head2 set_static_search_and_replace( GLOBALS )
416  
417 This method will apply the page_search_and_replace preference, and should be
418 run later than all of the other methods, to affect their results also.
419  
420 =cut
421  
422 ######################################################################
423  
424 0 ▶sub set_static_search_and_replace {
Subroutine set_static_search_and_replace: not called CC=1 SCAR=6.9
Pod documented
425 0 my ($self, $globals) = @_;
426 0 $globals->page_search_and_replace(
427 $globals->pref( 'page_search_and_replace' ) );
428 }
429  
430 ######################################################################
431  
432 1 1;
433 __END__
« DemoTextFile.pm ↑ Summary DemoGuestBook.pm »