blib/lib/CGI/FormBuilder/Source/File.pm untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 47 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 274 SCAR 70.8
CC
34
Cov
0%
parse
1190.0
new
2.0
write_module
2.0
CRAP 1190.0
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 use Carp;
25 use strict;
26 use warnings;
27 no warnings 'uninitialized';
28  
29 use 5.006; # or later
30 use CGI::FormBuilder::Util;
31  
32  
33 our $VERSION = '3.10';
34  
35 # Begin "real" code
36 sub new {
37 my $mod = shift;
38 my $class = ref($mod) || $mod;
39 my %opt = arghash(@_);
40 return bless \%opt, $class;
41 }
42  
43 sub parse {
44 local $^W = 0; # -w sucks so hard
45 my $self = shift;
46 my $file = shift || $self->{source};
47  
48 $CGI::FormBuilder::Util::DEBUG ||= $self->{debug} if ref $self;
49  
50 my $ret = {}; # top level
51 my $ptr = $ret; # curr ptr
52 my @lvl = (); # previous levels
53  
54 my $s = 0; # curr spaces
55 my $lsp = 0; # level spaces
56 my $psp = 0; # prev spaces
57  
58 my $refield = 0;
59 my @file;
60 my $utf8 = 0; # parse file as utf8
61  
62 debug 1, "parsing $file as input source";
63 if (ref $file eq 'SCALAR') {
64 @file = split /[\r\n]+/, $$file;
65 } elsif (ref $file eq 'ARRAY') {
66 @file = @$file;
67 } else {
68 open(F, "<$file") || puke "Cannot read $file: $!";
69 @file = <F>;
70 close F;
71 }
72  
73 my($lterm, $here); # level term, here string
74 my $inval = 0;
75 for (@file) {
76 next if /^\s*$/ || /^\s*#/; # blanks and comments
77 next if /^\s*\[\%\s*\#|^\s*-*\%\]/; # TT comments too
78 chomp;
79 my($term, $line) = split /\s*:\s*/, $_, 2;
80 $utf8 = 1 if $term eq 'charset' && $line =~ /^utf/; # key off charset to decode value
81 $line = Encode::decode('utf-8', $line) if $utf8;
82  
83 # here string term-inator (har)
84 if ($here) {
85 if ($term eq $here) {
86 undef $here;
87 next;
88 } else {
89 $line = $term;
90 $term = $lterm;
91 }
92 } else {
93 # count leading space if it's there
94 $s = 1; # reset
95 $s += length($1) if $term =~ s/^(\s+)//;
96 $line =~ s/\s+$//; # trailing space
97  
98 # uplevel pre-check (may have a value below)
99 if ($s == 1) {
100 $ptr = $ret;
101 @lvl = ();
102 $lsp = 1; # set to zero for next pass
103 $refield = 0;
104 $inval = 0;
105 } elsif ($s <= $lsp) {
106 $ptr = pop(@lvl) || $ret;
107 $lsp = $s; # uplevel term indent
108 $inval = 0;
109 }
110  
111 # special catch for continued (indented) line
112 if ($s >= $psp && $inval && ! length $line) {
113 $line = $term;
114 $term = $lterm;
115 }
116 debug 2, "[$s >= $psp, inval=$inval] term=$term; line=$line";
117 }
118 $psp = $s;
119  
120 # has a value
121 if (length $line) {
122 debug 2, "$term = $line ($s < $lsp)";
123  
124 $lsp ||= $s; # first valid term indent
125  
126 # <<HERE strings bypass all subsequent parsing
127 if ($line =~ /^<<(.+)/) {
128 $lterm = $term;
129 $here = $1;
130 next;
131 } elsif ($here) {
132 $ptr->{$term} .= "$line\n";
133 next;
134 }
135  
136 my @val;
137 if ($term =~ /^js/ || $term =~ /^on[a-z]/ || $term eq 'messages' || $term eq 'comment') {
138 @val = $line; # verbatim
139 } elsif ($line =~ s/^\\(.)//) {
140 # Reference - this is tricky. Go all the way up to
141 # the top to make sure, or use $self->{caller} if
142 # we were given a place to go.
143 my $r = $1;
144 my $l = 0;
145 my @p;
146 if ($self->{caller}) {
147 @p = $self->{caller};
148 } else {
149 while (my $pkg = caller($l++)) {
150 push @p, $pkg;
151 }
152 }
153 $line = "$r$p[-1]\::$line" unless $line =~ /::/;
154 debug 2, qq{eval "\@val = (\\$line)"};
155 eval "\@val = (\\$line)";
156 belch "Loading $line failed: $@" if $@;
157 } else {
158 # split commas
159 @val = split /\s*,\s*/, $line;
160  
161 # m=Male, f=Female -> [m,Male], [f,Female]
162 for (my $i=0; $i < @val; $i++) {
163 $val[$i] = [ split /\s*=\s*/, $val[$i], 2 ] if $val[$i] =~ /=/;
164 }
165 }
166  
167 # only arrayref on multi values b/c FB is "smart"
168 if ($ptr->{$term}) {
169 $ptr->{$term} = (ref $ptr->{$term})
170 ? [ @{$ptr->{$term}}, @val ] : @val > 1 ? \@val :
171 ref($val[0]) eq 'ARRAY' ? \@val : $val[0];
172 } else {
173 $ptr->{$term} = @val > 1 ? \@val : ref($val[0]) eq 'ARRAY' ? \@val : $val[0];
174 }
175 $inval = 1;
176 } else {
177 debug 2, "$term: new level ($s < $lsp)";
178  
179 # term:\n -> nest with bracket
180 if ($term eq 'fields') {
181 $refield = 1;
182 $term = 'fieldopts';
183 } elsif ($refield) {
184 push @{$ret->{fields}}, $term;
185 }
186  
187 $ptr->{$term} ||= {};
188 push @lvl, $ptr;
189 $ptr = $ptr->{$term};
190  
191 $lsp = $s; # reset spaces
192 $inval = 0;
193 }
194 $lterm = $term;
195 }
196  
197 if (ref $self) {
198 # add in any top-level options
199 while (my($k,$v) = each %$self) {
200 $ret->{$k} = $v unless exists $ret->{$k};
201 }
202  
203 # in FB, this is a class (not object) for speed
204 $self->{data} = $ret;
205 $self->{source} = $file;
206 }
207  
208 return wantarray ? %$ret : $ret;
209 }
210  
211 sub write_module {
212 my $self = shift;
213 my $mod = shift || puke "Missing required Module::Name";
214 (my $out = $mod) =~ s/.+:://;
215 $out .= '.pm';
216  
217 open(M, ">$out") || puke "Can't write $out: $!";
218  
219 print M "\n# Generated ".localtime()." by ".__PACKAGE__." $VERSION\n";
220 print M <<EOH;
221 #
222 # To use this, you must write a script and then use this module.
223 # In your script, get this form with "my \$form = $mod->new()"
224  
225 package $mod;
226  
227 use CGI::FormBuilder;
228 use strict;
229  
230 sub new {
231 # $mod->new() calling format
232 my \$self = shift if \@_ && \@_ % 2 != 0;
233  
234 # data structure from '$self->{source}'
235 EOH
236  
237 require Data::Dumper;
238 local $Data::Dumper::Varname = 'form';
239 print M " my ". Data::Dumper::Dumper($self->{data});
240  
241 print M <<'EOV';
242  
243 # allow overriding of individual parameters
244 while (@_) {
245 $form1->{shift()} = shift;
246 }
247  
248 # return a new form object
249 return CGI::FormBuilder->new(%$form1);
250 }
251  
252 1;
253 # End of module
254 EOV
255  
256 close M;
257 print STDERR "Wrote $out\n"; # send to stderr in case of httpd
258 }
259  
260 1;
261 __END__