DemoTextFile.pm

filter: Statementstmt 21.0% 27 / 128 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 16.6% 1 / 6 totaltotal 17.5% 33 / 188 CC 22 SCAR 38.8
CC
22
Cov
16%
get_curr_seg_content
110.0
main_dispatch
42.0
attach_document_navbar
30.0
CRAP 48.7
« CGI/Portable.pm ↑ Summary CGI/Portable/AppStatic.pm »
1 =head1 NAME
2  
3 DemoTextFile - Demo of CGI::Portable that displays the content of a
4 static html or text file, which can be in multiple segments.
5  
6 =cut
7  
8 ######################################################################
9  
10 package DemoTextFile;
11 1 require 5.004;
12  
13 # Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module
14 # is free software; you can redistribute it and/or modify it under the same terms
15 # as Perl itself. However, I do request that this copyright information and
16 # credits remain attached to the file. If you modify this module and
17 # redistribute a changed version then please attach a note listing the
18 # modifications. This module is available "as-is" and the author can not be held
19 # accountable for any problems resulting from its use.
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 $VERSION = '0.50';
25  
26 ######################################################################
27  
28 =head1 DEPENDENCIES
29  
30 =head2 Perl Version
31  
32 5.004
33  
34 =head2 Standard Modules
35  
36 I<none>
37  
38 =head2 Nonstandard Modules
39  
40 CGI::Portable 0.50
41 CGI::Portable::AppStatic 0.50
42  
43 =cut
44  
45 ######################################################################
46  
47 1 ▶use CGI::Portable 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use CGI::Portable::AppStatic 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 @ISA = qw(CGI::Portable::AppStatic);
50  
51 ######################################################################
52  
53 =head1 SYNOPSIS
54  
55 =head2 Display An HTML File By Itself
56  
57 #!/usr/bin/perl
58 use strict;
59 use warnings;
60  
61 require CGI::Portable;
62 my $globals = CGI::Portable->new();
63  
64 use Cwd;
65 $globals->file_path_root( cwd() ); # let us default to current working dir
66 $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );
67  
68 my %CONFIG = ( filename => 'intro.html' );
69  
70 $globals->set_prefs( \%CONFIG );
71 $globals->call_component( 'DemoTextFile' );
72  
73 require CGI::Portable::AdapterCGI;
74 my $io = CGI::Portable::AdapterCGI->new();
75 $io->send_user_output( $globals );
76  
77 1;
78  
79 =head2 Display A Plain Text File By Itself -- HTML Escaped
80  
81 my %CONFIG = ( filename => 'mycode.txt', is_text => 1 );
82  
83 =head2 Display A Text File In Multiple Segments With Headings
84  
85 my %CONFIG = (
86 title => 'Index of the World',
87 author => 'Jules Verne',
88 created => 'Version 1.0, first created 1993 June 24',
89 updated => 'Version 3.1, last modified 2000 November 18',
90 filename => 'jv_world.txt',
91 segments => 24,
92 is_text => 1,
93 );
94  
95 I<You need to have a subdirectory named "jv_world" that contains the 24 files
96 that correspond to the segments, named "jv_world_001.txt" through "...024.txt".>
97  
98 =head2 Display A Text File All On One Page With Headings
99  
100 my %CONFIG = (
101 title => 'Pizza Joints In New York',
102 author => 'Oscar Wilder',
103 created => 'Version 0.5, first created 1997 February 17',
104 updated => 'Version 1.2, last modified 1998 March 8',
105 filename => 'ow_pizza.txt',
106 segments => 1, # also the default
107 is_text => 1,
108 );
109  
110 I<You need to have a single file named "ow_pizza.txt", not in a subdirectory.>
111  
112 =head1 DESCRIPTION
113  
114 This Perl 5 object class is part of a demonstration of CGI::Portable in use.
115 It is one of a set of "application components" that takes its settings and user
116 input through CGI::Portable and uses that class to send its user output.
117 This demo module set can be used together to implement a web site complete with
118 static html pages, e-mail forms, guest books, segmented text document display,
119 usage tracking, and url-forwarding. Of course, true to the intent of
120 CGI::Portable, each of the modules in this demo set can be used independantly
121 of the others.
122  
123 =head1 SYNTAX
124  
125 This class does not export any functions or methods, so you need to call them
126 using object notation. This means using B<Class-E<gt>function()> for functions
127 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
128 your own modules, then that often means something like B<$self-E<gt>method()>.
129  
130 =head1 PUBLIC FUNCTIONS AND METHODS
131  
132 =head2 main( GLOBALS )
133  
134 You invoke this method to run the application component that is encapsulated by
135 this class. The required argument GLOBALS is an CGI::Portable object that
136 you have previously configured to hold the instance settings and user input for
137 this class. When this method returns then the encapsulated application will
138 have finished and you can get its user output from the CGI::Portable object.
139  
140 =head1 PREFERENCES HANDLED BY THIS MODULE
141  
142 I<This POD is coming when I get the time to write it.>
143  
144 filename # common part of filename for pieces
145 is_text # true if file is not html, but text
146 title # title of the document
147 author # who made the document
148 created # date and number of first version
149 updated # date and number of newest version
150 segments # number of pieces doc is in
151  
152 =cut
153  
154 ######################################################################
155  
156 # Names of properties for objects of this class are declared here:
157 1 my $KEY_SITE_GLOBALS = 'site_globals'; # hold global site values
158  
159 # Keys for items in site page preferences:
160 1 my $PKEY_FILENAME = 'filename'; # common part of filename for pieces
161 1 my $PKEY_IS_TEXT = 'is_text'; # true if file is not html, but text
162 1 my $PKEY_TITLE = 'title'; # title of the document
163 1 my $PKEY_AUTHOR = 'author'; # who made the document
164 1 my $PKEY_CREATED = 'created'; # date and number of first version
165 1 my $PKEY_UPDATED = 'updated'; # date and number of newest version
166 1 my $PKEY_SEGMENTS = 'segments'; # number of pieces doc is in
167  
168 ######################################################################
169  
170 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
Pod documented
171 0 my ($class, $globals) = @_;
172 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
173  
174 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
175 die "initializer is not a valid CGI::Portable object";
176  
177 0 $self->set_static_low_replace( $globals );
178  
179 0 $self->{$KEY_SITE_GLOBALS} = $globals;
180 0 $self->main_dispatch();
181  
182 0 $self->set_static_high_replace( $globals );
183 0 $self->set_static_attach_unordered( $globals );
184 0 $self->set_static_attach_ordered( $globals );
185 0 $self->set_static_search_and_replace( $globals );
186 }
187  
188 ######################################################################
189  
190 0 ▶sub main_dispatch {
Subroutine main_dispatch: not called CC=6 SCAR=37.4
Pod undocumented
191 0 my $self = shift( @_ );
192 0 my $globals = $self->{$KEY_SITE_GLOBALS};
193 0 my $rh_prefs = $globals->get_prefs_ref();
194
195 0 my $segments = $rh_prefs->{$PKEY_SEGMENTS};
196 0 ▶ $segments >= 1 or $rh_prefs->{$PKEY_SEGMENTS} = $segments = 1;
Branch: 1 branch
BranchTrueFalseTotal
$segments >= 1 or $rh_prefs->{$PKEY_SEGMENTS} = $segments = 1 0 0 0
197  
198 0 my $curr_seg_num = $globals->current_user_path_element();
199 0 ▶ $curr_seg_num >= 1 or $curr_seg_num = 1;
Branch: 1 branch
BranchTrueFalseTotal
$curr_seg_num >= 1 or $curr_seg_num = 1 0 0 0
200 0 ▶ $curr_seg_num <= $segments or $curr_seg_num = $segments;
Branch: 1 branch
BranchTrueFalseTotal
$curr_seg_num <= $segments or $curr_seg_num = $segments 0 0 0
201 0 $globals->current_user_path_element( $curr_seg_num );
202
203 0 $self->get_curr_seg_content();
204 0 ▶ if( $segments > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments > 1) 0 0 0
205 0 $self->attach_document_navbar();
206 }
207 0 ▶ if( $rh_prefs->{$PKEY_TITLE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_prefs->{$PKEY_TITLE}) 0 0 0
208 0 $self->attach_document_header();
209 }
210 }
211  
212 ######################################################################
213  
214 0 ▶sub get_curr_seg_content {
Subroutine get_curr_seg_content: not called CC=10 SCAR=47.0
Pod undocumented
215 0 my $self = shift( @_ );
216 0 my $globals = $self->{$KEY_SITE_GLOBALS};
217 0 my $rh_prefs = $globals->get_prefs_ref();
218 0 my $is_multi_segmented = $rh_prefs->{$PKEY_SEGMENTS} > 1;
219  
220 0 my ($base, $ext) = ($rh_prefs->{$PKEY_FILENAME} =~ m/^([^\.]*)(.*)$/);
221 0 ▶ my $seg_num_str = $is_multi_segmented ?
Branch: 1 branch
BranchTrueFalseTotal
$is_multi_segmented ? : 0 0 0
222 '_'.sprintf( "%3.3d", $globals->current_user_path_element() ) : '';
223  
224 0 my $filename = "$base$seg_num_str$ext";
225
226 0 ▶ my $physical_path = $globals->physical_filename(
Branch: 1 branch
BranchTrueFalseTotal
$is_multi_segmented ? : 0 0 0
227 $is_multi_segmented ? [$base, $filename] : $filename );
228  
229 SWITCH: {
230 0 $globals->add_no_error();
231  
232 0 ▶ open( STATIC, "<$physical_path" ) or do {
Branch: 1 branch
BranchTrueFalseTotal
open STATIC, "<$physical_path" or do { $globals->add_virtual_filename_error("open", $filename); last SWITCH } 0 0 0
233 0 $globals->add_virtual_filename_error( "open", $filename );
234 0 last SWITCH;
235 };
236 0 local $/ = undef;
237 0 ▶ defined( my $file_content = <STATIC> ) or do {
Branch: 1 branch
BranchTrueFalseTotal
defined(my $file_content = readline STATIC) or do { $globals->add_virtual_filename_error("read from", $filename); last SWITCH } 0 0 0
238 0 $globals->add_virtual_filename_error( "read from", $filename );
239 0 last SWITCH;
240 };
241 0 ▶ close( STATIC ) or do {
Branch: 1 branch
BranchTrueFalseTotal
close STATIC or do { $globals->add_virtual_filename_error("close", $filename); last SWITCH } 0 0 0
242 0 $globals->add_virtual_filename_error( "close", $filename );
243 0 last SWITCH;
244 };
245
246 0 ▶ if( $rh_prefs->{$PKEY_IS_TEXT} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rh_prefs->{$PKEY_IS_TEXT}) { } 0 0 0
elsif ($file_content =~ m[<body[^>]*>(.*)</body>]is) { } 0 0 0
247 0 $file_content =~ s/&/&amp;/g; # do some html escaping
248 0 $file_content =~ s/\"/&quot;/g;
249 0 $file_content =~ s/>/&gt;/g;
250 0 $file_content =~ s/</&lt;/g;
251
252 0 $globals->set_page_body(
253 [ "\n<pre>\n", $file_content, "\n</pre>\n" ] );
254
255 } elsif( $file_content =~ m|<body[^>]*>(.*)</body>|si ) {
256 0 $globals->set_page_body( $1 );
257 0 ▶ if( $file_content =~ m|<title>(.*)</title>|si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_content =~ m[<title>(.*)</title>]is) 0 0 0
258 0 $globals->page_title( $1 );
259 }
260 } else {
261 0 $globals->set_page_body( $file_content );
262 }
263 }
264  
265 0 ▶ if( $globals->get_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($globals->get_error) 0 0 0
266 0 $globals->page_title( 'Error Opening Page' );
267 0 $globals->set_page_body( <<__endquote );
268 0 <h1>@{[$globals->page_title()]}</h1>
269  
270 <p>I'm sorry, but an error has occurred while trying to open
271 the page you requested, which is in the file "$filename".</p>
272  
273 0 @{[$self->get_amendment_message()]}
274  
275 0 <p>Details: @{[$globals->get_error()]}</p>
276 __endquote
277  
278 0 $globals->add_no_error();
279 }
280 }
281  
282 ######################################################################
283  
284 0 ▶sub attach_document_navbar {
Subroutine attach_document_navbar: not called CC=5 SCAR=34.0
Pod undocumented
285 0 my $self = shift( @_ );
286 0 my $globals = $self->{$KEY_SITE_GLOBALS};
287  
288 0 my $segments = $globals->get_prefs_ref()->{$PKEY_SEGMENTS};
289 0 my $curr_seg_num = $globals->current_user_path_element();
290 0 my $seg_token = '__std_seg_token__';
291 0 my $common_url = $globals->url_as_string( $seg_token );
292  
293 0 my @seg_list_html = ();
294 0 foreach my $seg_num (1..$segments) {
295 0 ▶ if( $seg_num == $curr_seg_num ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg_num == $curr_seg_num) { } 0 0 0
296 0 push( @seg_list_html, "$seg_num\n" );
297 } else {
298 0 my $curr_seg_html = "<a href=\"$common_url\">$seg_num</a>\n";
299 0 $curr_seg_html =~ s/$seg_token/$seg_num/g;
300 0 push( @seg_list_html, $curr_seg_html );
301 }
302 }
303
304 0 ▶ my $prev_seg_html = ($curr_seg_num == 1) ? "Previous\n" :
Branch: 1 branch
BranchTrueFalseTotal
$curr_seg_num == 1 ? : 0 0 0
305 "<a href=\"$common_url\">Previous</a>\n";
306 0 $prev_seg_html =~ s/$seg_token/$curr_seg_num-1/ge;
307
308 0 ▶ my $next_seg_html = ($curr_seg_num == $segments) ? "Next\n" :
Branch: 1 branch
BranchTrueFalseTotal
$curr_seg_num == $segments ? : 0 0 0
309 "<a href=\"$common_url\">Next</a>\n";
310 0 $next_seg_html =~ s/$seg_token/$curr_seg_num+1/ge;
311
312 0 my $document_navbar =
313 <<__endquote.
314 <table><tr>
315 <td>$prev_seg_html</td><td>
316 __endquote
317  
318 join( ' | ', @seg_list_html ).
319  
320 <<__endquote;
321 </td><td>$next_seg_html</td>
322 </tr></table>
323 __endquote
324  
325 0 $globals->prepend_page_body( [$document_navbar] );
326 0 $globals->append_page_body( [$document_navbar] );
327 }
328  
329 ######################################################################
330  
331 0 ▶sub attach_document_header {
Subroutine attach_document_header: not called CC=2 SCAR=17.9
Pod undocumented
332 0 my $self = shift( @_ );
333 0 my $globals = $self->{$KEY_SITE_GLOBALS};
334 0 my $rh_prefs = $globals->get_prefs_ref();
335  
336 0 my $title = $rh_prefs->{$PKEY_TITLE};
337 0 my $author = $rh_prefs->{$PKEY_AUTHOR};
338 0 my $created = $rh_prefs->{$PKEY_CREATED};
339 0 my $updated = $rh_prefs->{$PKEY_UPDATED};
340 0 my $segments = $rh_prefs->{$PKEY_SEGMENTS};
341
342 0 my $curr_seg_num = $globals->current_user_path_element();
343 0 ▶ $title .= $segments > 1 ? ": $curr_seg_num / $segments" : '';
Branch: 1 branch
BranchTrueFalseTotal
$segments > 1 ? : 0 0 0
344
345 0 $globals->page_title( $title );
346  
347 0 $globals->prepend_page_body( <<__endquote );
348 0 <h1>@{[$globals->page_title()]}</h1>
349  
350 <p>Author: $author<br />
351 Created: $created<br />
352 Updated: $updated</p>
353 __endquote
354 }
355  
356 ######################################################################
357  
358 0 ▶sub get_amendment_message {
Subroutine get_amendment_message: not called CC=1 SCAR=6.9
Pod undocumented
359 0 my ($self) = shift( @_ );
360 0 my $globals = $self->{$KEY_SITE_GLOBALS};
361 0 return( <<__endquote );
362 <p>This should be temporary, the result of a transient server problem or an
363 0 update being performed at the moment. Click @{[$globals->recall_html('here')]}
364 to automatically try again. If the problem persists, please try again later,
365 0 or send an @{[$globals->maintainer_email_html('e-mail')]} message about the
366 problem, so it can be fixed.</p>
367 __endquote
368 }
369  
370 ######################################################################
371  
372 1 1;
373 __END__
« CGI/Portable.pm ↑ Summary CGI/Portable/AppStatic.pm »