lib/CGI/FormBuilder/Source/File.pm

filter: Statementstmt 87.1% 122 / 140 Branchbran 71.0% 54 / 76 Conditioncond 56.1% 23 / 41 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 74.1% 218 / 294 CC 54 SCAR 40.0
CC
54
Cov
77%
parse
66.4
write_module
12.0
new
2.1
CRAP 54.5
« lib/CGI/FormBuilder/Template/Builtin.pm ↑ Summary lib/CGI/FormBuilder/Multi.pm »
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 package CGI::FormBuilder::Source::File;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Source::File - Initialize FormBuilder from external file
12  
13 =head1 SYNOPSIS
14  
15 # use the main module
16 use CGI::FormBuilder;
17  
18 my $form = CGI::FormBuilder->new(source => 'form.conf');
19  
20 my $lname = $form->field('lname'); # like normal
21  
22 =cut
23  
24 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 ▶use 5.006; # or later
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 2 our $VERSION = '3.20';
35  
36 # Begin "real" code
37 23 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
38 23 my $mod = shift;
39 23 ▶ my $class = ref($mod) || $mod;
Condition33%1/3
ref $mod || $mod
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $mod || $mod
ref $mod $mod
Truth table33%1/3
A || B
A= ref $modB= $mod
ABresult
1X1
011
000
40 23 my %opt = arghash(@_);
41 23 return bless \%opt, $class;
42 }
43  
44 23 ▶sub parse {
Subroutine parse: called CC=51 SCAR=42.0
Pod documented
45 23 local $^W = 0; # -w sucks so hard
46 23 my $self = shift;
47 23 ▶ my $file = shift || $self->{source};
Condition33%1/3
shift() || $self->{'source'}
l!l&&r!l&&!r
0230
MC/DC0%0/2
shift() || $self->{'source'}
shift() $self->{'source'}
Truth table33%1/3
A || B
A= shift()B= $self->{'source'}
ABresult
1X1
011
000
48  
49 23 ▶ $CGI::FormBuilder::Util::DEBUG ||= $self->{debug} if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 23 0 23
Condition33%1/3
$CGI::FormBuilder::Util::DEBUG ||= $self->{"debug"}
l!l&&r!l&&!r
0023
MC/DC0%0/2
$CGI::FormBuilder::Util::DEBUG ||= $self->{"debug"}
$CGI::FormBuilder::Util::DEBUG $self->{"debug"}
Truth table33%1/3
A ||= B
A= $CGI::FormBuilder::Util::DEBUGB= $self->{"debug"}
ABresult
1X1
011
000
50  
51 23 my $ret = {}; # top level
52 23 my $ptr = $ret; # curr ptr
53 23 my @lvl = (); # previous levels
54  
55 23 my $s = 0; # curr spaces
56 23 my $lsp = 0; # level spaces
57 23 my $psp = 0; # prev spaces
58  
59 23 my $refield = 0;
60 23 my @file;
61 23 my $utf8 = 0; # parse file as utf8
62  
63 23 debug 1, "parsing $file as input source";
64 23 ▶ if (ref $file eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 23 0 23
elsif (ref $file eq 'ARRAY') { } 0 0 0
65 23 @file = split /[\r\n]+/, $$file;
66 } elsif (ref $file eq 'ARRAY') {
67 0 @file = @$file;
68 } else {
69 0 ▶ open(F, "<$file") || puke "Cannot read $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "<$file" or &puke("Cannot read ${file}: $!") 0 0 0
70 0 @file = <F>;
71 0 close F;
72 }
73  
74 23 my($lterm, $here); # level term, here string
75 23 my $inval = 0;
76 23 for (@file) {
77 239 ▶ next if /^\s*$/ || /^\s*#/; # blanks and comments
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ or /^\s*#/ 25 214 239
Condition100%3/3
/^\s*$/ or /^\s*#/
l!l&&r!l&&!r
196214
MC/DC100%2/2
/^\s*$/ or /^\s*#/
/^\s*$/ /^\s*#/
Truth table100%3/3
A or B
A= /^\s*$/B= /^\s*#/
ABresult
1X1
011
000
78 214 ▶ next if /^\s*\[\%\s*\#|^\s*-*\%\]/; # TT comments too
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\[\%\s*\#|^\s*-*\%\]/ 0 214 214
79 214 chomp;
80 214 my($term, $line) = split /\s*:\s*/, $_, 2;
81 214 ▶ $utf8 = 1 if $term eq 'charset' && $line =~ /^utf/; # key off charset to decode value
Branch: 1 branch
BranchTrueFalseTotal
if $term eq "charset" and $line =~ /^utf/ 0 214 214
Condition33%1/3
$term eq "charset" and $line =~ /^utf/
!ll&&!rl&&r
21400
MC/DC0%0/2
$term eq "charset" and $line =~ /^utf/
$term eq "charset" $line =~ /^utf/
Truth table33%1/3
A and B
A= $term eq "charset"B= $line =~ /^utf/
ABresult
0X0
100
111
82 214 ▶ $line = utf8::decode($line) if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 0 214 214
83  
84 # here string term-inator (har)
85 214 ▶ if ($here) {
Branch: 1 branch
BranchTrueFalseTotal
if ($here) { } 3 211 214
86 3 ▶ if ($term eq $here) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term eq $here) { } 1 2 3
87 1 undef $here;
88 1 next;
89 } else {
90 2 $line = $term;
91 2 $term = $lterm;
92 }
93 } else {
94 # count leading space if it's there
95 211 $s = 1; # reset
96 211 ▶ $s += length($1) if $term =~ s/^(\s+)//;
Branch: 1 branch
BranchTrueFalseTotal
if $term =~ s/^(\s+)// 120 91 211
97 211 $line =~ s/\s+$//; # trailing space
98  
99 # uplevel pre-check (may have a value below)
100 211 ▶ if ($s == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s == 1) { } 91 120 211
elsif ($s <= $lsp) { } 24 96 120
101 91 $ptr = $ret;
102 91 @lvl = ();
103 91 $lsp = 1; # set to zero for next pass
104 91 $refield = 0;
105 91 $inval = 0;
106 } elsif ($s <= $lsp) {
107 24 ▶ $ptr = pop(@lvl) || $ret;
Condition33%1/3
pop @lvl || $ret
l!l&&r!l&&!r
2400
MC/DC0%0/2
pop @lvl || $ret
pop @lvl $ret
Truth table33%1/3
A || B
A= pop @lvlB= $ret
ABresult
1X1
011
000
108 24 $lsp = $s; # uplevel term indent
109 24 $inval = 0;
110 }
111  
112 # special catch for continued (indented) line
113 211 ▶ if ($s >= $psp && $inval && ! length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s >= $psp and $inval and not length $line) 1 210 211
Condition100%6/6
$s >= $psp and $inval and not length $line
!ll&&!rl&&r
178321
$s >= $psp and $inval
!ll&&!rl&&r
5112733
MC/DC100%3/3
$s >= $psp and $inval and not length $line
$s >= $psp $inval not length $line
Truth table100%4/4
A and B and not(C)
A= $s >= $pspB= $invalC= not length $line
ABCresult
0XX0
10X0
1100
1111
114 1 $line = $term;
115 1 $term = $lterm;
116 }
117 211 debug 2, "[$s >= $psp, inval=$inval] term=$term; line=$line";
118 }
119 213 $psp = $s;
120  
121 # has a value
122 213 ▶ if (length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) { } 132 81 213
123 132 debug 2, "$term = $line ($s < $lsp)";
124  
125 132 ▶ $lsp ||= $s; # first valid term indent
Condition33%1/3
$lsp ||= $s
l!l&&r!l&&!r
13200
MC/DC0%0/2
$lsp ||= $s
$lsp $s
Truth table33%1/3
A ||= B
A= $lspB= $s
ABresult
1X1
011
000
126  
127 # <<HERE strings bypass all subsequent parsing
128 132 ▶ if ($line =~ /^<<(.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^<<(.+)/) { } 1 131 132
elsif ($here) { } 2 129 131
129 1 $lterm = $term;
130 1 $here = $1;
131 1 next;
132 } elsif ($here) {
133 2 $ptr->{$term} .= "$line\n";
134 2 next;
135 }
136  
137 129 my @val;
138 129 ▶ if ($term =~ /^js/ || $term =~ /^on[a-z]/ || $term eq 'messages' || $term eq 'comment') {
Branch: 2 branches
BranchTrueFalseTotal
if ($term =~ /^js/ or $term =~ /^on[a-z]/ or $term eq 'messages' or $term eq 'comment') { } 8 121 129
elsif ($line =~ s/^\\(.)//) { } 2 119 121
Condition77%7/9
$term =~ /^js/ or $term =~ /^on[a-z]/ or $term eq "messages" or $term eq "comment"
l!l&&r!l&&!r
53121
$term =~ /^js/ or $term =~ /^on[a-z]/ or $term eq "messages"
l!l&&r!l&&!r
50124
$term =~ /^js/ or $term =~ /^on[a-z]/
l!l&&r!l&&!r
50124
MC/DC50%2/4
$term =~ /^js/ or $term =~ /^on[a-z]/ or $term eq "messages" or $term eq "comment"
$term =~ /^js/ $term =~ /^on[a-z]/ $term eq "messages" $term eq "comment"
Truth table60%3/5
A or B or C or D
A= $term =~ /^js/B= $term =~ /^on[a-z]/C= $term eq "messages"D= $term eq "comment"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
139 8 @val = $line; # verbatim
140 } elsif ($line =~ s/^\\(.)//) {
141 # Reference - this is tricky. Go all the way up to
142 # the top to make sure, or use $self->{caller} if
143 # we were given a place to go.
144 2 my $r = $1;
145 2 my $l = 0;
146 2 my @p;
147 2 ▶ if ($self->{caller}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'caller'}) { } 0 2 2
148 0 @p = $self->{caller};
149 } else {
150 2 ▶ while (my $pkg = caller($l++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkg = caller $l++) 4 2 6
151 4 push @p, $pkg;
152 }
153 }
154 2 ▶ $line = "$r$p[-1]\::$line" unless $line =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /::/ 2 0 2
155 2 debug 2, qq{eval "\@val = (\\$line)"};
156 2 eval "\@val = (\\$line)";
157 2 ▶ belch "Loading $line failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
158 } else {
159 # split commas
160 119 @val = split /\s*,\s*/, $line;
161  
162 # m=Male, f=Female -> [m,Male], [f,Female]
163 119 ▶ for (my $i=0; $i < @val; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @val) 221 119 340
164 221 ▶ $val[$i] = [ split /\s*=\s*/, $val[$i], 2 ] if $val[$i] =~ /=/;
Branch: 1 branch
BranchTrueFalseTotal
if $val[$i] =~ /=/ 25 196 221
165 }
166 }
167  
168 # only arrayref on multi values b/c FB is "smart"
169 129 ▶ if ($ptr->{$term}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr->{$term}) { } 1 128 129
170 $ptr->{$term} = (ref $ptr->{$term})
171 1 ▶ ? [ @{$ptr->{$term}}, @val ] : @val > 1 ? \@val :
Branch: 3 branches
BranchTrueFalseTotal
ref $ptr->{$term} ? : 1 0 1
@val > 1 ? : 0 0 0
ref $val[0] eq 'ARRAY' ? : 0 0 0
172 ref($val[0]) eq 'ARRAY' ? \@val : $val[0];
173 } else {
174 128 ▶ $ptr->{$term} = @val > 1 ? \@val : ref($val[0]) eq 'ARRAY' ? \@val : $val[0];
Branch: 2 branches
BranchTrueFalseTotal
@val > 1 ? : 30 98 128
ref $val[0] eq 'ARRAY' ? : 1 97 98
175 }
176 129 $inval = 1;
177 } else {
178 81 debug 2, "$term: new level ($s < $lsp)";
179  
180 # term:\n -> nest with bracket
181 81 ▶ if ($term eq 'fields') {
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq 'fields') { } 15 66 81
elsif ($refield) { } 41 25 66
182 15 $refield = 1;
183 15 $term = 'fieldopts';
184 } elsif ($refield) {
185 41 push @{$ret->{fields}}, $term;
186 }
187  
188 81 ▶ $ptr->{$term} ||= {};
Condition50%1/2
$ptr->{$term} ||= {}
l!l
081
MC/DC0%0/1
$ptr->{$term} ||= {}
$ptr->{$term}
Truth table50%1/2
A
A= $ptr->{$term}
Aresult
11
00
189 81 push @lvl, $ptr;
190 81 $ptr = $ptr->{$term};
191  
192 81 $lsp = $s; # reset spaces
193 81 $inval = 0;
194 }
195 210 $lterm = $term;
196 }
197  
198 23 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 23 0 23
199 # add in any top-level options
200 23 ▶ while (my($k,$v) = each %$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 23 23 46
201 23 ▶ $ret->{$k} = $v unless exists $ret->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ret->{$k} 23 0 23
202 }
203  
204 # in FB, this is a class (not object) for speed
205 23 $self->{data} = $ret;
206 23 $self->{source} = $file;
207 }
208  
209 23 ▶ return wantarray ? %$ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 23 23
210 }
211  
212 0 ▶sub write_module {
Subroutine write_module: not called CC=3 SCAR=24.8
Pod documented
213 0 my $self = shift;
214 0 ▶ my $mod = shift || puke "Missing required Module::Name";
Condition0%0/3
shift() || &puke('Missing required Module::Name')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || &puke('Missing required Module::Name')
shift() &puke('Missing required Module::Name')
Truth table0%0/3
A || B
A= shift()B= &puke('Missing required Module::Name')
ABresult
1X1
011
000
215 0 (my $out = $mod) =~ s/.+:://;
216 0 $out .= '.pm';
217  
218 0 ▶ open(M, ">$out") || puke "Can't write $out: $!";
Branch: 1 branch
BranchTrueFalseTotal
open M, ">$out" or &puke("Can't write ${out}: $!") 0 0 0
219  
220 0 print M "\n# Generated ".localtime()." by ".__PACKAGE__." $VERSION\n";
221 0 print M <<EOH;
222 #
223 # To use this, you must write a script and then use this module.
224 # In your script, get this form with "my \$form = $mod->new()"
225  
226 package $mod;
227  
228 use CGI::FormBuilder;
229 use strict;
230  
231 sub new {
232 # $mod->new() calling format
233 my \$self = shift if \@_ && \@_ % 2 != 0;
234  
235 # data structure from '$self->{source}'
236 EOH
237  
238 0 require Data::Dumper;
239 0 local $Data::Dumper::Varname = 'form';
240 0 print M " my ". Data::Dumper::Dumper($self->{data});
241  
242 0 print M <<'EOV';
243  
244 # allow overriding of individual parameters
245 while (@_) {
246 $form1->{shift()} = shift;
247 }
248  
249 # return a new form object
250 return CGI::FormBuilder->new(%$form1);
251 }
252  
253 1;
254 # End of module
255 EOV
256  
257 0 close M;
258 0 print STDERR "Wrote $out\n"; # send to stderr in case of httpd
259 }
260  
261 2 1;
262 __END__
« lib/CGI/FormBuilder/Template/Builtin.pm ↑ Summary lib/CGI/FormBuilder/Multi.pm »