blib/lib/Text/TemplateFill.pm

filter: Statementstmt 79.6% 246 / 309 Branchbran 55.0% 77 / 140 Conditioncond 44.4% 20 / 45 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 82.1% 23 / 28 Podpod 42.8% 9 / 21 totaltotal 66.4% 379 / 570 CC 90 SCAR 25.7
CC
90
Cov
69%
Expand
33.7
Error
20.0
GetVarDets
15.2
CRAP 13.1
↑ Summary
1 # This module generates text output from template files, filling in value fields.
2 # /\
3 # / \ (C) Copyright 2002-2003 Parliament Hill Computers Ltd.
4 # \ / All rights reserved.
5 # \/
6 # . Author: Alain Williams, July 2002
7 # . addw@phcomp.co.uk
8 # .
9 # .
10 #
11 # SCCS: @(#)TemplateFill.pm 1.7 03/27/03 10:27:28
12 # Alain D D Williams <addw@phcomp.co.uk>, July 2002
13 #
14 # This module is free software; you can redistribute it and/or modify
15 # it under the same terms as Perl itself. You must preserve this entire copyright
16 # notice in any use or distribution.
17 # The author makes no warranty what so ever that this code works or is fit
18 # for purpose: you are free to use this code on the understanding that any problems
19 # are your responsibility.
20  
21 # Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is
22 # hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and
23 # this permission notice appear in supporting documentation.
24  
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package Text::TemplateFill;
28  
29 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Math::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # What local variables - visible elsewhere
34 1 ▶use vars qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
35 @ISA @EXPORT
36 1 /;
37
38 1 @ISA = ('Exporter');
39  
40 1 @EXPORT = qw(
41 $VERSION
42 );
43  
44 1 our $VERSION = "1.7";
45  
46 # This contains the current (and probably only) template instance.
47 # It is used where we can't guess it, this relies on only being invoked for one
48 # instance at a time:
49 1 my $globalself;
50 my $globaltag; # And for current tag
51  
52 1 my $ArithIdent; # Identification string for arithmetic - in case of errors
53  
54 # Nothing at startup
55 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 ;
57 }
58  
59 # Nothing at end
60 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
61 ;
62 }
63  
64 # Default error output function
65 0 ▶sub PrintError {
Subroutine PrintError: not called CC=1 SCAR=6.9
Pod undocumented
66 0 printf STDERR @_;
67 0 print STDERR "\n";
68 }
69  
70 # Called when something is wrong.
71 # Args: $self, fprintf style args
72 # The point is that the error function is called.
73 # Nasty bit to avoid reporting functions in this module.
74 # Take care to switch back to the calling locale - if we set one.
75 0 ▶sub Error {
Subroutine Error: not called CC=4 SCAR=30.0
Pod undocumented
76 0 my $self = shift @_;
77 0 my ($pack,$file,$line,$sub,$hargs,undef,$eval,$require);
78 0 my $i = 1;
79 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$pack eq "Text::TemplateFill" and do { do { () = caller $i++ } } 0 0 0
80 0 ($pack,$file,$line,$sub,$hargs,undef,$eval,$require) = caller($i++);
81 } while($pack eq 'Text::TemplateFill');
82  
83 0 my $fmt = shift @_;
84 0 my $OldLocale;
85  
86 0 ▶ if($self->{OldLocale} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OldLocale"} ne "") 0 0 0
87 0 $OldLocale = setlocale(LC_CTYPE);
88 0 setlocale(LC_ALL, $self->{OldLocale});
89 }
90  
91 0 $self->{ErrorFunction}($fmt . ". Called from $file line $line fn: $pack ", @_);
92  
93 0 ▶ setlocale(LC_ALL, $OldLocale) if(defined($OldLocale));
Branch: 1 branch
BranchTrueFalseTotal
if defined $OldLocale 0 0 0
94  
95 0 $self->{Errors}++;
96 }
97  
98 # Print errors from arithmetic, rely on $globalself
99 # Prepend the offending statement at the start of the error message, makes it a bit long I am afraid:
100 0 ▶sub ArithError {
Subroutine ArithError: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $fmt = shift @_;
102 0 &Error($globalself, "Calc '%s' " . $fmt, $ArithIdent, @_);
103 }
104  
105 # Return the value of a variable - return an array
106 # 0 Magic value to Math::Expression
107 # 1 Variable name
108 # NB: Math::Expression holds vars as arrays, this uses simple scalars.
109 20 ▶sub ArithVarGet {
Subroutine ArithVarGet: called CC=2 SCAR=7.0
Pod undocumented
110 20 my ($self, $name) = @_;
111  
112 20 my $varref = &Value($globalself, $globaltag, $name, 0);
113 20 my @v = ( );
114 20 ▶ push @v, ${$varref} if(defined($varref));
Branch: 1 branch
BranchTrueFalseTotal
if defined $varref 20 0 20
115  
116 20 return @v;
117 }
118  
119 # Is a variable is defined - return 1 or 0
120 # 0 Magic value to Math::Expression
121 # 1 Variable name
122 0 ▶sub ArithVarIsDef {
Subroutine ArithVarIsDef: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my ($self, $name) = @_;
124  
125 0 my $varref = &Value($globalself, $globaltag, $name, 0);
126  
127 0 ▶ return defined($varref) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $varref ? : 0 0 0
128 }
129  
130 # Set the value of a variable - return the array
131 # 0 Magic value to Math::Expression
132 # 1 Variable name
133 # 2 Value - array
134 48 ▶sub ArithVarSet {
Subroutine ArithVarSet: called CC=1 SCAR=0.0
Pod undocumented
135 48 my ($self, $name, @val) = @_;
136  
137 48 my $varref = &Value($globalself, $globaltag, $name, 1);
138  
139 48 ${$varref} = $val[$#val];
140  
141 48 return @val;
142 }
143  
144 # Create a new template object.
145 # Initialise default options.
146 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
147 2 my $class = shift;
148  
149 2 my $PageNo = 0; # Not started yet. The first to print is page 1
150 2 my $PageLineNo = 0;
151 2 my $Now = time;
152  
153 # Program variables
154 2 my %ProgVars = (
155 );
156  
157 # Calculated & auto vars
158 2 my %CalcVars = (
159 'PageNo' => \$PageNo, # Current page number
160 'PageLineNo' => \$PageLineNo, # Current line number in page (0 if page not started)
161 'Now' => \$Now, # Current time - actually time of 'new'.
162 );
163  
164 2 my $CalcHandle = new Math::Expression;
165 2 $CalcHandle->SetOpt( 'VarGetFun' => \&ArithVarGet,
166 'VarSetFun' => \&ArithVarSet,
167 'VarIsDefFun' => \&ArithVarIsDef,
168 'PrintErrFunc' => \&ArithError);
169  
170 # References to all the paragraphs for this template
171 # This contains references to hashes keyed on the paragraph tag.
172 2 my %Paragraphs = (
173 );
174  
175 2 my %template = (
176 'Errors' => 0, # Error count
177 'ErrorFunction' => \&PrintError, # What to call on error
178 'Initialised' => 0, # True when post-read file initialisation done
179 'Locale' => '', # May be something like 'fr_CA.ISO8859-1'
180 'OldLocale' => '', # On entry to GeneratePara
181 'BaseDir' => '.', # What to add if no '/' at start of file name
182 'LineTerminator'=> "\n", # What to put at the end of each line
183 'EndPageSeq' => '', # Probably \f, else use many empty lines
184 'PageLen' => 66, # Length of output page
185 'Variables' => \%ProgVars, # Hash of all program variables
186 'CalcVars' => \%CalcVars, # Hash of all calculated variables
187 'Paragraphs' => \%Paragraphs, # Hash of all paragraphs
188 'StartPageTag' => ' ', # Which tag to use to auto start a page, undef doesn't work - space is dodge
189 'EndPageTag' => ' ', # Which tag to use to auto end a page
190 'CalcHandle' => $CalcHandle, # For calculations
191 );
192  
193 2 return bless \%template => $class;
194 }
195  
196 # Set an option in the %template.
197 2 ▶sub SetOpt {
Subroutine SetOpt: called CC=4 SCAR=14.0
Pod documented
198 2 my $self = shift @_;
199  
200 2 ▶ while($#_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) 6 2 8
201 6 ▶ &Error($self, "Unknown option '$_[0]'") unless(defined($self->{$_[0]}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{$_[0]} 0 6 6
202 6 ▶ &Error($self, "No value to option '$_[0]'") unless(defined($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 6 6
203 6 $self->{$_[0]} = $_[1];
204 6 shift;shift;
205 }
206 }
207  
208 # Read a file in. The user arguments are:
209 # * a tag name that is used to identify/obtain this paragraph in the future
210 # * a file name that will be read, if this is not given, use tag.
211 # Return true on error.
212 10 ▶sub ReadPara {
Subroutine ReadPara: called CC=14 SCAR=27.2
Pod documented
213 10 my ($self, $tag, $fname) = @_;
214  
215 10 $globalself = $self;
216  
217 10 $self->{Initialised} = 0; # Need to reassess what is what
218  
219 10 ▶ $fname = $tag unless(defined($fname)); # no $fname ?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fname 10 0 10
220  
221 10 my @Lines = ();
222 10 my ($ParaOnPage, $ParaTotal) = (0, 0);
223 10 my @Calc = ();
224 10 my @CalcStr = ();
225 10 my @LineNoMap = ();
226  
227 10 my %ParaDescript = (
228 'Lines' => \@Lines,
229 'LineNoMap' => \@LineNoMap, # For error messages - else removed comments cause bad reporting of errors
230 'Calc' => \@Calc, # Calculations - parsed trees
231 'CalcStr' => \@CalcStr, # Calculations - uncompiled
232 'EndPage' => 0, # True if paragaph ends a page
233 'StartPage' => 0, # True if paragaph starts a page
234 'BlanksAfter' => 0, # True blanks to page botton when EndPage come after paragraph
235 'ParaOnPage' => \$ParaOnPage, # Paragraph usage count this page
236 'ParaTotal' => \$ParaTotal, # Paragraph usage count total
237 );
238  
239 # Get the file to open, prepend the base dir if not absolute:
240 10 ▶ my $fn = (($fname =~ /^\//) ? '' : $self->{BaseDir}) . '/' . $fname;
Branch: 1 branch
BranchTrueFalseTotal
$fname =~ m[^/] ? : 0 10 10
241  
242 10 ▶ unless(open(TMPL, "<$fn")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open TMPL, "<$fn") 0 10 10
243 0 &Error($self, "Cannot open '%s' as: $!", $fn);
244 0 return(1);
245 }
246  
247 10 ▶ while(<TMPL>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TMPL)) 287 10 297
248 287 chop; # Basic line tidy:
249 287 s/\s*\r?$//;
250 287 ▶ next if(/^\$\{#\}/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\$\{#\}/ 98 189 287
251  
252 # If it is a calculation, extract it & save
253 189 ▶ if(/^\$\{Calc\s+(.*)\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\$\{Calc\s+(.*)\}$/) 17 172 189
254 17 push @CalcStr, "$fn:$. '$1'";
255 17 push @Calc, $self->{CalcHandle}->Parse("$1");
256 17 next;
257 }
258  
259 # If not an option, append to template lines:
260 172 ▶ unless(/^\$\{Opt\s/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\$\{Opt\s/) 154 18 172
261 154 push @Lines, $_;
262 154 push @LineNoMap, $.;
263 154 next;
264 }
265  
266 # Process options:
267 18 ▶ unless(/^\$\{Opt\s+(\w+)\s*([^\s]+)?\s*\}/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\$\{Opt\s+(\w+)\s*([^\s]+)?\s*\}/) 0 18 18
268 0 &Error($self, "Bad option line $. in '%s'", $fn);
269 0 next;
270 }
271  
272 18 my ($optkey, $optval) = ($1, $2);
273  
274 # The option will be held as a member of a hash, find which hash
275 18 my $href;
276 18 ▶ $href = \%ParaDescript if(defined($ParaDescript{$optkey}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ParaDescript{$optkey} 12 6 18
277 18 ▶ $href = $self if(defined($self->{$optkey}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$optkey} 6 12 18
278 18 ▶ unless(defined($href)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $href) 0 18 18
279 0 &Error($self, "Unknown option '$optkey' line $. in '%s'", $fn);
280 0 next;
281 }
282  
283 # No validation on the value, if none just set to true
284 18 ▶ $href->{$optkey} = defined($optval) ? ($optval eq "''" ? '' : $optval) : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $optval ? : 6 12 18
$optval eq q[''] ? : 0 6 6
285 }
286  
287 10 close(TMPL);
288  
289 10 $self->{Paragraphs}{$tag} = \%ParaDescript;
290  
291 10 return(0);
292 }
293  
294 # This checks what has been read & deduces:
295 # * What tag to use at the start of a page
296 # * What tag to use to end a page
297 2 ▶sub CompleteInit {
Subroutine CompleteInit: called CC=6 SCAR=21.2
Pod undocumented
298 2 my $self = $_[0];
299
300 2 foreach my $para (keys %{$self->{Paragraphs}}) {
301  
302 10 my $parh = ${$self->{Paragraphs}}{$para};
303  
304 # NB: there is an important difference between the tag having a space value & having the empty value.
305 # The space value is a dodge to say that it is unset - ugh, empty means deliberately no tag.
306 $self->{StartPageTag} = $para if($self->{StartPageTag} eq ' ' and
307 0 ▶ ${$parh}{'StartPage'} != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"StartPageTag"} eq " " and do { $parh }->{"StartPage"} != 0 0 10 10
Condition33%1/3
$self->{"StartPageTag"} eq " " and do { $parh }->{"StartPage"} != 0
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->{"StartPageTag"} eq " " and do { $parh }->{"StartPage"} != 0
$self->{"StartPageTag"} eq " " do { $parh }->{"StartPage"} != 0
Truth table33%1/3
A and B
A= $self->{"StartPageTag"} eq " "B= do { $parh }->{"StartPage"} != 0
ABresult
0X0
100
111
308 $self->{EndPageTag} = $para if($self->{EndPageTag} eq ' ' and
309 0 ▶ ${$parh}{'EndPage'} != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EndPageTag"} eq " " and do { $parh }->{"EndPage"} != 0 0 10 10
Condition33%1/3
$self->{"EndPageTag"} eq " " and do { $parh }->{"EndPage"} != 0
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->{"EndPageTag"} eq " " and do { $parh }->{"EndPage"} != 0
$self->{"EndPageTag"} eq " " do { $parh }->{"EndPage"} != 0
Truth table33%1/3
A and B
A= $self->{"EndPageTag"} eq " "B= do { $parh }->{"EndPage"} != 0
ABresult
0X0
100
111
310 }
311  
312 2 $self->{Initialised} = 1;
313 }
314  
315 # Reset all page/line counters to zero.
316 4 ▶sub Reset {
Subroutine Reset: called CC=2 SCAR=6.9
Pod documented
317 4 my $self = $_[0];
318  
319 4 ${$self->{CalcVars}{PageLineNo}} = 0;
320 4 ${$self->{CalcVars}{PageNo}} = 0;
321  
322 # Reset paragraph usage on this page to 0
323 4 foreach my $para (keys %{$self->{Paragraphs}}) {
324 20 ${$self->{Paragraphs}{$para}{ParaOnPage}} = 0;
325 20 ${$self->{Paragraphs}{$para}{ParaTotal}} = 0;
326 }
327 }
328  
329 # This is called to start a page.
330 # Args: $self, optional tag to start the page with - else the defined start tag - if there is one
331 # This assumes that any previous page is complete.
332 # Return something to print
333 4 ▶sub StartPage {
Subroutine StartPage: called CC=6 SCAR=19.1
Pod documented
334 4 my ($self, $tag) = @_;
335  
336 # Need this in case called before a GeneratePara
337 4 ▶ &CompleteInit($self) unless($self->{Initialised});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Initialised"} 0 4 4
338  
339 4 ▶ $tag = $self->{StartPageTag} unless(defined($tag));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 4 4
340  
341 4 ${$self->{CalcVars}{PageLineNo}} = 1; # Line number of first line to print
342 4 ${$self->{CalcVars}{PageNo}}++;
343  
344 # Reset paragraph usage on this page to 0
345 4 foreach my $para (keys %{$self->{Paragraphs}}) {
346 20 ${$self->{Paragraphs}{$para}{ParaOnPage}} = 0;
347 }
348  
349 # If there is a start page tag, output it:
350 4 ▶ return(&GeneratePara($self, $tag)) if($tag ne ' ' and $tag ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne " " and $tag ne "" 4 0 4
Condition33%1/3
$tag ne " " and $tag ne ""
!ll&&!rl&&r
004
MC/DC0%0/2
$tag ne " " and $tag ne ""
$tag ne " " $tag ne ""
Truth table33%1/3
A and B
A= $tag ne " "B= $tag ne ""
ABresult
0X0
100
111
351 0 return('');
352 }
353  
354 # End the current page.
355 # Args: $self, optional tag to end the page with - else the defined end tag - if there is one
356 # If there is nothing on the page, print an empty page with a footer.
357 4 ▶sub EndPage {
Subroutine EndPage: called CC=9 SCAR=24.4
Pod documented
358 4 my ($self, $tag) = @_;
359  
360 4 my $text = '';
361 # Page not started ? Get it going but don't print a header:
362 4 ▶ $text = &StartPage($self, '') if(${$self->{CalcVars}{PageLineNo}} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"CalcVars"}{"PageLineNo"};} < 1 0 4 4
363  
364 4 ▶ $tag = $self->{EndPageTag} unless(defined($tag));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 4 4
365  
366 # Work out how empty many lines we must generate to put the footer in the right place.
367 4 my $lines = $self->{PageLen} - ${$self->{CalcVars}{PageLineNo}};
368  
369 # If end of page tag: blank line down to it (or after), else o/p a formfeed or blank down to end of page:
370 4 ▶ if($tag ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne '') { } 4 0 4
371 4 my $blanksafter = $self->{Paragraphs}{$tag}{BlanksAfter};
372 4 my $blanklines = $self->{LineTerminator} x ($lines - $#{$self->{Paragraphs}{$tag}{Lines}});
373 4 ▶ $text .= $blanklines unless($blanksafter);
Branch: 1 branch
BranchTrueFalseTotal
unless $blanksafter 0 4 4
374 4 ▶ $text .= &GeneratePara($self, $tag) if($tag ne ' ' and $tag ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne " " and $tag ne "" 4 0 4
Condition33%1/3
$tag ne " " and $tag ne ""
!ll&&!rl&&r
004
MC/DC0%0/2
$tag ne " " and $tag ne ""
$tag ne " " $tag ne ""
Truth table33%1/3
A and B
A= $tag ne " "B= $tag ne ""
ABresult
0X0
100
111
375 4 ▶ $text .= $blanklines if($blanksafter);
Branch: 1 branch
BranchTrueFalseTotal
if $blanksafter 4 0 4
376 } else {
377 0 ▶ $text .= ($self->{EndPageSeq} ne '') ? $self->{EndPageSeq} : ($self->{LineTerminator} x $lines);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'EndPageSeq'} ne '' ? : 0 0 0
378 }
379  
380 4 ${$self->{CalcVars}{PageLineNo}} = 0; # Next page not started
381  
382 4 return $text;
383 }
384  
385 # Print an end page if there is something on the current page.
386 # Args: $self, optional tag to end the page with - else the defined end tag - if there is one
387 # Don't print anything if the current page has not been started - should only be at start of file.
388 # If a tag is specified and it is not the default end page tag, a check will be made to see if the
389 # specified paragraph will fit on the page, if not a standard endpage/startpage is first done.
390 4 ▶sub CompletePage {
Subroutine CompletePage: called CC=6 SCAR=20.5
Pod documented
391 4 my ($self, $tag) = @_;
392  
393 4 ▶ return '' if(${$self->{CalcVars}{PageLineNo}} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"CalcVars"}{"PageLineNo"};} < 1 0 4 4
394  
395 4 my $text = '';
396  
397 # Won't fit
398 $text = &EndPage($self) if(defined($tag) and $tag ne $self->{EndPageTag} and $self->{PageLen} > 0 and
399 4 ▶ ${$self->{CalcVars}{PageLineNo}} + $#{${$self->{Paragraphs}{$tag}}{Lines}} >= $self->{PageLen});
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag and $tag ne $self->{"EndPageTag"} and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{${$$self{"Paragraphs"}{$tag};}{"Lines"};} >= $self->{"PageLen"} 0 4 4
Condition33%3/9
defined $tag and $tag ne $self->{"EndPageTag"} and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{${$$self{"Paragraphs"}{$tag};}{"Lines"};} >= $self->{"PageLen"}
!ll&&!rl&&r
040
defined $tag and $tag ne $self->{"EndPageTag"} and $self->{"PageLen"} > 0
!ll&&!rl&&r
004
defined $tag and $tag ne $self->{"EndPageTag"}
!ll&&!rl&&r
004
MC/DC0%0/4
defined $tag and $tag ne $self->{"EndPageTag"} and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{${$$self{"Paragraphs"}{$tag};}{"Lines"};} >= $self->{"PageLen"}
defined $tag $tag ne $self->{"EndPageTag"} $self->{"PageLen"} > 0 ${$self->{"CalcVars"}{"PageLineNo"};} + $#{${$$self{"Paragraphs"}{$tag};}{"Lines"};} >= $self->{"PageLen"}
Truth table20%1/5
A and B and C and D
A= defined $tagB= $tag ne $self->{"EndPageTag"}C= $self->{"PageLen"} > 0D= ${$self->{"CalcVars"}{"PageLineNo"};} + $#{${$$self{"Paragraphs"}{$tag};}{"Lines"};} >= $self->{"PageLen"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
400  
401 4 return $text . &EndPage($self, $tag);
402 }
403  
404 # Evaluate the paragraph and return an array that can be printed.
405 # Return the empty string
406 18 ▶sub GeneratePara {
Subroutine GeneratePara: called CC=14 SCAR=27.1
Pod documented
407 18 my ($self, $tag) = @_;
408 18 my $para = $self->{Paragraphs}{$tag};
409 18 my $text = '';
410  
411 18 $self->{OldLocale} = '';
412  
413 # print "GeneratePara '$tag' self='$self'\n";
414 18 ▶ unless(defined($para)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $para) 0 18 18
415 0 &Error($self, "Tag '%s' is not known", $tag);
416 0 return('');
417 }
418 18 my $lines = ${$para}{Lines};
419  
420 18 ▶ &CompleteInit($self) unless($self->{Initialised}); # Once off after files read
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Initialised"} 2 16 18
421  
422 # Need to end the page if the current paragraph will not fit on the page - NOT if printing EOP para
423 # Count lines left, lines this para, lines in EndPage. Not if Line/page < 1
424 $text .= &EndPage($self) if($para->{EndPage} == 0 and $self->{PageLen} > 0 and
425 14 ▶ ${$self->{CalcVars}{PageLineNo}} + $#{$lines} +
Branch: 1 branch
BranchTrueFalseTotal
$self->{'EndPageTag'} ne '' ? : 14 0 14
426 18 ▶ ($self->{EndPageTag} ne '' ? $#{${$self->{Paragraphs}{$self->{EndPageTag}}}{Lines}} : 0) >= $self->{PageLen});
Branch: 1 branch
BranchTrueFalseTotal
if $para->{"EndPage"} == 0 and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{$lines;} + ($self->{"EndPageTag"} ne "" ? $#{${$self->{"Paragraphs"}{$self->{"EndPageTag"}};}{"Lines"};} : 0) >= $self->{"PageLen"} 0 18 18
Condition66%4/6
$para->{"EndPage"} == 0 and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{$lines;} + ($self->{"EndPageTag"} ne "" ? $#{${$self->{"Paragraphs"}{$self->{"EndPageTag"}};}{"Lines"};} : 0) >= $self->{"PageLen"}
!ll&&!rl&&r
4140
$para->{"EndPage"} == 0 and $self->{"PageLen"} > 0
!ll&&!rl&&r
4014
MC/DC0%0/3
$para->{"EndPage"} == 0 and $self->{"PageLen"} > 0 and ${$self->{"CalcVars"}{"PageLineNo"};} + $#{$lines;} + ($self->{"EndPageTag"} ne "" ? $#{${$self->{"Paragraphs"}{$self->{"EndPageTag"}};}{"Lines"};} : 0) >= $self->{"PageLen"}
$para->{"EndPage"} == 0 $self->{"PageLen"} > 0 ${$self->{"CalcVars"}{"PageLineNo"};} + $#{$lines;} + ($self->{"EndPageTag"} ne "" ? $#{${$self->{"Paragraphs"}{$self->{"EndPageTag"}};}{"Lines"};} : 0) >= $self->{"PageLen"}
Truth table50%2/4
A and B and C
A= $para->{"EndPage"} == 0B= $self->{"PageLen"} > 0C= ${$self->{"CalcVars"}{"PageLineNo"};} + $#{$lines;} + ($self->{"EndPageTag"} ne "" ? $#{${$self->{"Paragraphs"}{$self->{"EndPageTag"}};}{"Lines"};} : 0) >= $self->{"PageLen"}
ABCresult
0XX0
10X0
1100
1111
427  
428 # Need to start a page and this paragraph is not a start of page paragraph ?
429 # Even if $tag is a StartPage we need to call &StartPage to get page # increment, etc.
430 18 ▶ if(${$self->{CalcVars}{PageLineNo}} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"CalcVars"}{"PageLineNo"};} <= 0) 4 14 18
431 4 ▶ $text .= &StartPage($self, ($para->{StartPage} == 0 ? undef : $tag));
Branch: 1 branch
BranchTrueFalseTotal
$para->{'StartPage'} == 0 ? : 0 4 4
432 4 ▶ return $text if($para->{StartPage}); # Else we get the start page text twice
Branch: 1 branch
BranchTrueFalseTotal
if $para->{"StartPage"} 4 0 4
433 }
434  
435 14 ${$para->{ParaOnPage}}++;
436 14 ${$para->{ParaTotal}}++;
437  
438 # Perform any calculations first:
439 14 $globalself = $self; # For arithmetic
440 14 $globaltag = $tag;
441 14 my $calc = $para->{Calc};
442 14 ▶ &Calculate($self, $tag, $calc, $para->{CalcStr}) if($#{$calc} >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$calc;} >= 0 14 0 14
443  
444 # Change locale if one is defined
445 14 ▶ if($self->{Locale} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Locale"} ne "") 14 0 14
446 14 $self->{OldLocale} = setlocale(LC_CTYPE);
447 14 setlocale(LC_ALL, $self->{Locale});
448 }
449  
450 14 my $lineno = 0; # For error messages
451 14 foreach my $line (@{$lines}) {
452 244 my $exp_line = &Expand($self, $para, $line, $tag, ${$para}{LineNoMap}[$lineno]);
453 244 $text .= $exp_line . $self->{LineTerminator};
454 244 ${$self->{CalcVars}{PageLineNo}}++;
455 244 $lineno++;
456 }
457  
458 # Change locale back
459 14 ▶ setlocale(LC_ALL, $self->{OldLocale}) if($self->{OldLocale} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OldLocale"} ne "" 14 0 14
460 14 $self->{OldLocale} = '';
461  
462 14 return $text;
463 }
464  
465 # Perform calculations for a paragraph
466 14 ▶sub Calculate {
Subroutine Calculate: called CC=2 SCAR=6.9
Pod undocumented
467 14 my ($self, $tag, $calc, $calcstr) = @_;
468 14 my $para = $self->{Paragraphs}{$tag};
469  
470 14 ▶ for(my $i = 0; $i <= $#{$calc}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$calc;}) 24 14 38
471 24 my $tree = ${$calc}[$i];
472 24 $ArithIdent = ${$calcstr}[$i]; # For errors
473 24 $self->{CalcHandle}->EvalToScalar($tree);
474 }
475 }
476  
477 # Return the value of a variable or constant.
478 # This is for use in expression evaluation.
479 # NB: $self is for Math::Expression, so rely on $globalself.
480 # undefOK is set if we are assigning.
481 68 ▶sub Value {
Subroutine Value: called CC=2 SCAR=7.1
Pod undocumented
482 68 my ($self, $tag, $val, $undefOK) = @_;
483 68 my $para = $globalself->{Paragraphs}{$tag};
484  
485 68 my ($tn, $vn);
486 68 ▶ if($val =~ /^\$(\w+)\.?(\w+)?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\$(\w+)\.?(\w+)?/) { } 0 68 68
487 0 ($tn, $vn) = ($1, $2);
488 } else {
489 68 ($tn, $vn) = ($val, undef);
490 }
491  
492 68 my ($varref, $varname) = &GetVarDets($globalself, $tag, $tn, $vn, "Bad expression value in paragraph '$tag'", $undefOK);
493  
494 68 return $varref;
495 }
496  
497 # Expand a line - internal function.
498 244 ▶sub Expand {
Subroutine Expand: called CC=15 SCAR=35.2
Pod undocumented
499 244 my ($self, $para, $line, $tag, $lineno) = @_;
500 244 my $newline = '';
501  
502 # print "expand '$line'\n";
503 # Extract ${value@conversion<conversion_arg>%format}
504  
505 # This is nasty:
506 244 ▶ while($line =~ s/^
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^ ([^\$]*) # Any non dollar \$\{(\w+)\.?(\w+)? # ${Variable or ${Tag.Variable (@(\w+)\s*(<([^>]+)>)?)? # Opt: @conversion <conv_opt> (%[-+ #]*\d*.?\d*\w+)? # %PrintfFormat \}//x) 104 244 348
507 ([^\$]*) # Any non dollar
508 \$\{(\w+)\.?(\w+)? # ${Variable or ${Tag.Variable
509 (@(\w+)\s*(<([^>]+)>)?)? # Opt: @conversion <conv_opt>
510 (%[-+ #]*\d*.?\d*\w+)? # %PrintfFormat
511 \}//x) {
512 104 $newline .= $1;
513 104 my $tn = $2;
514 104 my $vn = $3;
515 104 my $conv = $5;
516 104 my $conv_opt = $7;
517 104 my $format = $8;
518  
519 104 my ($substv, $varname) = &GetVarDets($self, $tag, $tn, $vn, "Line $lineno of paragraph '$tag'", 0);
520  
521 104 ▶ unless(defined($substv)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $substv) 0 104 104
522 0 $newline .= $varname; # Couldn't get a value for variable
523 0 next;
524 }
525  
526 # Special conversions:
527 104 ▶ if(defined($conv) and $conv ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conv and $conv ne "") 12 92 104
Condition66%2/3
defined $conv and $conv ne ""
!ll&&!rl&&r
92012
MC/DC50%1/2
defined $conv and $conv ne ""
defined $conv $conv ne ""
Truth table66%2/3
A and B
A= defined $convB= $conv ne ""
ABresult
0X0
100
111
528 12 ▶ if($conv eq 'time') {
Branch: 2 branches
BranchTrueFalseTotal
if ($conv eq 'time') { } 12 0 12
elsif ($conv eq 'center') { } 0 0 0
529 # Convert using a date style format string
530 12 ▶ $conv_opt = '%c' unless(defined($conv_opt)); # Locale preferred conversion
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conv_opt 0 12 12
531  
532 12 my $newval = strftime($conv_opt, localtime ${$substv});
533 12 $substv = \$newval;
534 } elsif($conv eq 'center') {
535 # Center a field in a specified width
536 0 ▶ $conv_opt = 1 unless(defined($conv_opt) and ($conv_opt =~ /^\d+/));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conv_opt and $conv_opt =~ /^\d+/ 0 0 0
Condition0%0/3
defined $conv_opt and $conv_opt =~ /^\d+/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conv_opt and $conv_opt =~ /^\d+/
defined $conv_opt $conv_opt =~ /^\d+/
Truth table0%0/3
A and B
A= defined $conv_optB= $conv_opt =~ /^\d+/
ABresult
0X0
100
111
537 0 my $len = length(${$substv});
538  
539 0 ▶ if($len < $conv_opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $conv_opt) 0 0 0
540 0 my $newval = (' ' x (($conv_opt - $len) / 2)) . ${$substv} . (' ' x (($conv_opt - $len + 1) / 2));
541 0 $substv = \$newval;
542 }
543 } else {
544 0 &Error($self, "Line $lineno of paragraph '%s' uses unknown conversion '%s'", $tag, $conv);
545 0 $newline .= '${' . $vn . '@' . $conv . '}';
546 0 next;
547 }
548 }
549  
550 # print "substv='$substv' vn='$vn' format='$format'\n";
551 # Format
552 104 ▶ if(defined($format) and $format ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format and $format ne '') { } 68 36 104
Condition66%2/3
defined $format and $format ne ""
!ll&&!rl&&r
36068
MC/DC50%1/2
defined $format and $format ne ""
defined $format $format ne ""
Truth table66%2/3
A and B
A= defined $formatB= $format ne ""
ABresult
0X0
100
111
553 # If numeric avoid barf on unassigned var:
554 68 ▶ if(${$substv} eq '' and ($format =~ /[duoxegfXEGiDUOF]$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$substv;} eq "" and $format =~ /[duoxegfXEGiDUOF]$/) 0 68 68
Condition33%1/3
${$substv;} eq "" and $format =~ /[duoxegfXEGiDUOF]$/
!ll&&!rl&&r
6800
MC/DC0%0/2
${$substv;} eq "" and $format =~ /[duoxegfXEGiDUOF]$/
${$substv;} eq "" $format =~ /[duoxegfXEGiDUOF]$/
Truth table33%1/3
A and B
A= ${$substv;} eq ""B= $format =~ /[duoxegfXEGiDUOF]$/
ABresult
0X0
100
111
555 0 my $z = 0;
556 0 $substv = \$z;
557 }
558 68 $newline .= sprintf $format, ${$substv};
559 } else {
560 36 $newline .= ${$substv};
561 }
562 }
563  
564 244 return $newline . $line;
565 }
566  
567 # Find out things about a variable and return:
568 # * the reference to the variable, undefined on error
569 # * the variable name, in a form suitable for error display
570 # Args:
571 # 0 $self
572 # 1 Paragraph tag
573 # 2 tagname -- extracted from tagname.varname
574 # 3 varname
575 # 4 Error message prefix
576 # 5 True if undefined value is OK (in which case set the empty string) (must be a calc var)
577 172 ▶sub GetVarDets {
Subroutine GetVarDets: called CC=11 SCAR=27.2
Pod undocumented
578 172 my ($self, $tag, $tn, $vn, $msg, $undefOK) = @_;
579 172 my $para = $self->{Paragraphs}{$tag};
580  
581 172 my $varref;
582 my $varname;
583  
584 # If no Variable, the Tag is the variable
585 172 ▶ if(defined($vn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vn) { } 8 164 172
586 8 $varname = $tn . '.' . $vn;
587  
588 8 ▶ unless(defined($self->{Paragraphs}{$tn})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Paragraphs'}{$tn}) { } 8 0 8
589 0 &Error($self, "%s uses unknown tag '%s' in template variable '%s'", $msg, $tag, $varname);
590 } else {
591 8 ▶ unless(defined($varref = $self->{Paragraphs}{$tn}{$vn})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($varref = $self->{"Paragraphs"}{$tn}{$vn})) 0 8 8
592 0 ▶ if($undefOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undefOK) { } 0 0 0
593 0 my $empty = '';
594 0 $varref = $self->{Paragraphs}{CalcVars}{$vn} = \$empty;
595 } else {
596 0 &Error($self, "%s uses unknown template variable '%s'", $msg, $varname);
597 }
598 }
599 }
600 } else {
601 # Get ref to value - page's own, calculated or global:
602 164 $varref = $self->{Variables}{$tn};
603 164 ▶ $varref = $self->{CalcVars}{$tn} if(defined($self->{CalcVars}{$tn}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"CalcVars"}{$tn} 61 103 164
604 164 ▶ $varref = $para->{$tn} if(defined($para->{$tn}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $para->{$tn} 0 164 164
605 164 ▶ if(!defined($varref) and $undefOK) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $varref and $undefOK) 9 155 164
Condition66%2/3
not defined $varref and $undefOK
!ll&&!rl&&r
15509
MC/DC50%1/2
not defined $varref and $undefOK
not defined $varref $undefOK
Truth table66%2/3
not(A) and B
A= not defined $varrefB= $undefOK
ABresult
0X0
100
111
606 9 my $empty = '';
607 9 $varref = $self->{CalcVars}{$tn} = \$empty;
608 }
609 164 $varname = $tn;
610 164 ▶ &Error($self, "%s uses unknown template variable '%s'", $msg, $varname)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $varref 0 164 164
611 unless(defined($varref));
612 }
613  
614 # Is the value of the variable defined ?
615 172 ▶ unless(defined(${$varref})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$varref;}) 0 172 172
616 0 &Error($self, "%s uses variable '%s' which does not have a value", $msg, $varname);
617 0 undef $varref;
618 }
619  
620 172 return ($varref, ('${' . $varname . '}'));
621 }
622  
623 # Set variable_name/variable association.
624 # The argument is an array of name => variable_reference
625 # Because we take a reference, this only needs to be called once in a program.
626 # A warning will be made if a name is reused.
627 2 ▶sub BindVars {
Subroutine BindVars: called CC=4 SCAR=14.8
Pod documented
628 2 my $self = shift @_;
629  
630 2 my $varp = $self->{Variables};
631  
632 2 ▶ while((my $name = shift @_) and (my $val = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @_ and my $val = shift @_) 32 2 34
Condition66%2/3
my $name = shift @_ and my $val = shift @_
!ll&&!rl&&r
2032
MC/DC50%1/2
my $name = shift @_ and my $val = shift @_
my $name = shift @_ my $val = shift @_
Truth table66%2/3
A and B
A= my $name = shift @_B= my $val = shift @_
ABresult
0X0
100
111
633 # print "name='$name' val='${$val}'\n";
634 32 ▶ if(defined($varp->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $varp->{$name}) 0 32 32
635 0 &Error($self, "Reusing variable name '%s'", $name);
636 0 $self->{Errors}--; # Only warning
637 }
638 32 $varp->{$name} = $val;
639 }
640 }
641  
642 # Remove variable_name/variable association.
643 # The argument is an array of names
644 0 ▶sub UnbindVars {
Subroutine UnbindVars: not called CC=3 SCAR=24.8
Pod documented
645 0 my $self = shift @_;
646  
647 0 my $varp = $self->{Variables};
648  
649 0 ▶ while((my $name = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @_) 0 0 0
650 # print "name='$name' val='${$val}'\n";
651 0 ▶ if(defined($varp->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $varp->{$name}) { } 0 0 0
652 0 delete($varp->{$name});
653 } else {
654 0 &Error($self, "Ubinding unknown variable name '%s'", $name);
655 0 $self->{Errors}--; # Only warning
656 }
657 }
658 }
659  
660 1 1;
661  
662 __END__
↑ Summary