blib/lib/HTML/FillInForm/Lite.pm

filter: Statementstmt 99.5% 244 / 245 Branchbran 97.6% 123 / 126 Conditioncond 96.4% 27 / 28 MC/DCmcdc 88.8% 16 / 18 Subroutinesub 100% 30 / 30 Podpod 100% 3 / 3 totaltotal 98.4% 443 / 450 CC 75 SCAR 21.9
CC
75
Cov
99%
_parse_option
21.0
fill
12.0
_fill_input
10.0
CRAP 8.9
« blib/lib/HTML/FillInForm/Lite/Compat.pm ↑ Summary Build »
1 package HTML::FillInForm::Lite;
2 18 ▶use 5.008_001; # 5.8.1
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 our $VERSION = '1.14';
8  
9 18 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 our @ISA = qw(Exporter);
11 18 our @EXPORT_OK = qw(fillinform);
12  
13 #use Smart::Comments '####';
14 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Regexp for HTML tags
18  
19 18 my $form = q{[fF][oO][rR][mM]};
20 18 my $input = q{[iI][nN][pP][uU][tT]};
21 18 my $select = q{[sS][eE][lL][eE][cC][tT] };
22 18 my $option = q{[oO][pP][tT][iI][oO][nN] };
23 18 my $textarea = q{[tT][eE][xX][tT][aA][rR][eE][aA]};
24  
25 18 my $checked = q{[cC][hH][eE][cC][kK][eE][dD]};
26 18 my $selected = q{[sS][eE][lL][eE][cC][tT][eE][dD]};
27 18 my $multiple = q{[mM][uU][lL][tT][iI][pP][lL][eE]};
28  
29 18 my $id = q{[iI][dD]};
30 18 my $type = q{[tT][yY][pP][eE]};
31 18 my $name = q{[nN][aA][mM][eE]};
32 18 my $value = q{[vV][aA][lL][uU][eE]};
33  
34 18 my $SPACE = q{\s};
35 18 my $ATTR_NAME = q{[\w\-]+};
36 18 my $ATTR_VALUE = q{(?:" [^"]* " | ' [^']* ' | [^'"/>\s]+ | [\w\-]+ )};
37 18 my $ATTR = qq{(?: $SPACE+ (?: $ATTR_NAME (?: = $ATTR_VALUE )? ) )};
38  
39 18 my $FORM = qq{(?: <$form $ATTR+ $SPACE* > )}; # <form>
40 18 my $INPUT = qq{(?: <$input $ATTR+ $SPACE*/?> )}; # <input>
41 18 my $SELECT = qq{(?: <$select $ATTR+ $SPACE* > )}; # <select>
42 18 my $OPTION = qq{(?: <$option $ATTR* $SPACE* > )}; # <option>
43 18 my $TEXTAREA = qq{(?: <$textarea $ATTR+ $SPACE* > )}; # <textarea>
44  
45 18 my $END_FORM = qq{(?: </$form> )};
46 18 my $END_SELECT = qq{(?: </$select> )};
47 18 my $END_OPTION = qq{(?: </$option> )};
48 18 my $END_TEXTAREA = qq{(?: </$textarea> )};
49  
50 18 my $CHECKED = qq{(?:
51 $checked (?: = (?: "$checked " | '$checked' | $checked ) )?
52 )};
53 18 my $SELECTED = qq{(?:
54 $selected (?: = (?: "$selected" | '$selected' | $selected ) )?
55 )};
56 18 my $MULTIPLE = qq{(?:
57 $multiple (?: = (?: "$multiple" | '$multiple' | $multiple ) )?
58 )};
59  
60 #my $DISABLED = q{(?: disabled = (?: "disabled" | 'disabled' | disabled ) )};
61  
62 #sub _extract{ # for debugging only
63 # my $s = shift;
64 # my %f = (input => [], select => [], textarea => []);
65 # @{$f{input}} = $s =~ m{($INPUT)}gxmsi;
66 # @{$f{select}} = $s =~ m{($SELECT.*?$END_SELECT)}gxmsi;
67 # @{$f{textarea}} = $s =~ m{($TEXTAREA.*?$END_TEXTAREA)}gxmsi;
68 #
69 # return \%f;
70 #}
71  
72  
73 sub fillinform { # function interface to fill()
74 5 ▶ if(@_ == 1) {
Subroutine fillinform: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 4 5
75 1 my($data) = @_;
76 1 my $fif = __PACKAGE__->new();
77 return sub {
78 2 ▶ my($form) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
79 2 return $fif->fill(\$form, $data);
80 }
81 1 }
82 else {
83 4 my($form, $data) = @_;
84 4 return __PACKAGE__->fill(\$form, $data);
85 }
86 }
87  
88 # utilities for getting HTML attributes
89 sub _unquote{
90 414 ▶ $_[0] =~ /(['"]) (.*) \1/xms ? $2 : $_[0]; # ' for poor editors
Subroutine _unquote: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /(['"]) (.*) \1/msx ? : 407 7 414
91 }
92 sub _get_id{
93 22 ▶ $_[0] =~ /$SPACE $id = ($ATTR_VALUE)/xms ? _unquote($1) : undef;
Subroutine _get_id: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$SPACE $id = ($ATTR_VALUE)/msx ? : 20 2 22
94 }
95 sub _get_type{
96 178 ▶ $_[0] =~ /$SPACE $type = ($ATTR_VALUE)/xms ? _unquote($1) : undef;
Subroutine _get_type: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$SPACE $type = ($ATTR_VALUE)/msx ? : 91 87 178
97 }
98 sub _get_name{
99 231 ▶ $_[0] =~ /$SPACE $name = ($ATTR_VALUE)/xms ? _unquote($1) : undef;
Subroutine _get_name: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$SPACE $name = ($ATTR_VALUE)/msx ? : 228 3 231
100 }
101 sub _get_value{
102 118 ▶ $_[0] =~ /$SPACE $value = ($ATTR_VALUE)/xms ? _unquote($1) : undef;
Subroutine _get_value: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$SPACE $value = ($ATTR_VALUE)/msx ? : 75 43 118
103 }
104  
105 #use macro
106 # _unquote => \&_unquote,
107 # _get_id => \&_get_id,
108 # _get_type => \&_get_type,
109 # _get_name => \&_get_name,
110 # _get_value => \&_get_value,
111 #;
112  
113 sub new :method{
114 33 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
115 33 return $class->_parse_option(@_);
116 }
117  
118 sub _parse_option{
119 244 ▶ my $self = shift;
Subroutine _parse_option: called CC=21 SCAR=30.5
120  
121 244 ▶ if(ref $self and not @_){ # as instance method with no option
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and not @_) 141 103 244
Condition100%3/3
ref $self and not @_
!ll&&!rl&&r
5746141
MC/DC100%2/2
ref $self and not @_
ref $self not @_
Truth table100%3/3
A and not(B)
A= ref $selfB= not @_
ABresult
0X0
100
111
122 141 return $self;
123 }
124  
125 103 my %context = (
126 ignore_types => {
127 button => 1,
128 submit => 1,
129 reset => 1,
130 password => 1,
131 image => 1,
132 file => 1,
133 },
134  
135 escape => \&_escape_html,
136 decode_entity => \&_noop,
137 layer => '',
138 );
139  
140 # merge if needed
141 103 ▶ if(ref $self){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 46 57 103
142 46 ▶ while(my($key, $val) = each %{$self}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self;}) 215 46 261
143 215 ▶ $context{$key} = ref($val) eq 'HASH' ? { %{$val} } : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'HASH' ? : 68 147 215
144 }
145 }
146  
147 # parse options
148 103 ▶ while(my($opt, $val) = splice @_, 0, 2){
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 92 101 193
149 92 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 91 92
150  
151 91 ▶ if( $opt eq 'ignore_fields'
Branch: 6 branches
BranchTrueFalseTotal
if ($opt eq 'ignore_fields' or $opt eq 'disable_fields') { } 8 83 91
elsif ($opt eq 'fill_password') { } 27 56 83
elsif ($opt eq 'target') { } 21 35 56
elsif ($opt eq 'escape') { } 3 32 35
elsif ($opt eq 'layer') { } 0 32 32
elsif ($opt eq 'decode_entity') { } 30 2 32
Condition100%3/3
$opt eq "ignore_fields" or $opt eq "disable_fields"
l!l&&r!l&&!r
7183
MC/DC100%2/2
$opt eq "ignore_fields" or $opt eq "disable_fields"
$opt eq "ignore_fields" $opt eq "disable_fields"
Truth table100%3/3
A or B
A= $opt eq "ignore_fields"B= $opt eq "disable_fields"
ABresult
1X1
011
000
152 or $opt eq 'disable_fields' ){
153 8 ▶ @{ $context{$opt} ||= {} }{ @{$val} }
Condition100%2/2
$context{$opt} ||= {}
l!l
35
MC/DC100%1/1
$context{$opt} ||= {}
$context{$opt}
Truth table100%2/2
A
A= $context{$opt}
Aresult
11
00
154 8 = (1) x @{$val};
155 }
156 elsif($opt eq 'fill_password'){
157 27 $context{ignore_types}{password} = !$val;
158 }
159 elsif($opt eq 'target'){
160 21 $context{target} = $val;
161 }
162 elsif($opt eq 'escape'){
163 3 ▶ if($val){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 2 1 3
164 2 ▶ $context{escape} = ref($val) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 1 1 2
165 ? $val
166 : \&_escape_html;
167 }
168 else{
169 1 $context{escape} = \&_noop;
170 }
171 }
172 elsif($opt eq 'layer'){
173 0 $context{layer} = $val;
174 }
175 elsif($opt eq 'decode_entity'){
176 30 ▶ if($val){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 28 2 30
177 28 ▶ $context{decode_entity} = ref($val) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 1 27 28
178 ? $val
179 : \&_decode_entity;
180 }
181 else{
182 2 $context{decode_entity} = \&_noop;
183 }
184 }
185 else{
186 2 Carp::croak("Unknown option '$opt' supplied");
187 }
188 }
189  
190 101 ▶ return bless \%context => ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
46550
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
191 }
192  
193 sub fill :method{
194 216 ▶ my($self, $src, $q, @opt) = @_;
Subroutine fill: called CC=12 SCAR=24.8
Pod documented
195  
196 216 ▶ defined $src or Carp::croak('No source supplied');
Branch: 1 branch
BranchTrueFalseTotal
defined $src or Carp::croak("No source supplied") 3 213 216
197 213 ▶ defined $q or Carp::croak('No data supplied');
Branch: 1 branch
BranchTrueFalseTotal
defined $q or Carp::croak("No data supplied") 2 211 213
198  
199 211 my $context = $self->_parse_option(@opt);
200  
201 ### $context
202  
203 # HTML source to a scalar
204 210 my $content;
205 210 ▶ if(ref($src) eq 'SCALAR'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $src eq 'SCALAR') { } 190 20 210
elsif (ref $src eq 'ARRAY') { } 7 13 20
206 190 $content = ${$src}; # copy
207 }
208 elsif(ref($src) eq 'ARRAY'){
209 7 $content = join q{}, @{$src};
210 }
211 else{
212 13 my $is_fh = Scalar::Util::openhandle($src);
213  
214 13 ▶ if($is_fh or !ref($src)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_fh or not ref $src) { } 11 2 13
Condition100%3/3
$is_fh or not ref $src
l!l&&r!l&&!r
562
MC/DC100%2/2
$is_fh or not ref $src
$is_fh not ref $src
Truth table100%3/3
A or not(B)
A= $is_fhB= not ref $src
ABresult
1X1
011
000
215 11 ▶ if(!$is_fh){
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_fh) 6 5 11
216 6 ▶ open my($in), '<'.$context->{layer}, $src
Branch: 1 branch
BranchTrueFalseTotal
open my $in, '<' . $context->{"layer"}, $src or Carp::croak("Cannot open '${src}': $!") 1 5 6
217 or Carp::croak("Cannot open '$src': $!");
218 5 $src = $in;
219 }
220 10 local $/;
221 10 $content = readline($src); # slurp
222 }
223 else {
224 2 $content = ${$src};
225 }
226 }
227  
228 # if $content is utf8-flagged, params should be utf8-encoded
229 208 local $context->{utf8} = utf8::is_utf8($content);
230  
231 # param object converted from data or object
232 208 local $context->{data} = _to_form_object($q);
233  
234 # param storage for multi-text fields
235 203 local $context->{params} = {};
236  
237 # Fill in contents
238 203 ▶ if(defined $context->{target}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{'target'}) { } 21 182 203
239  
240 21 $content =~ s{ ($FORM) (.*?) ($END_FORM) }
241 {
242 22 my($beg, $content, $end) = ($1, $2, $3);
243  
244 22 my $id = _get_id($beg);
245 22 ▶ (defined($id) and $context->{target} eq $id)
Branch: 1 branch
BranchTrueFalseTotal
defined $id && $context->{'target'} eq $id ? : 11 11 22
Condition100%3/3
defined $id and $context->{"target"} eq $id
!ll&&!rl&&r
2911
MC/DC100%2/2
defined $id and $context->{"target"} eq $id
defined $id $context->{"target"} eq $id
Truth table100%3/3
A and B
A= defined $idB= $context->{"target"} eq $id
ABresult
0X0
100
111
246 ? $beg . _fill($context, $content) . $end
247 : $beg . $content . $end
248 }gexms;
249  
250 21 return $content;
251 }
252 else{
253 182 return _fill($context, $content);
254 }
255  
256 }
257  
258 sub _fill{
259 193 ▶ my($context, $content) = @_;
Subroutine _fill: called CC=1 SCAR=0.0
260 193 $content =~ s{($INPUT)}
261 178 { _fill_input($context, $1) }gexms;
262  
263 193 $content =~ s{($SELECT) (.*?) ($END_SELECT) }
264 43 { $1 . _fill_select($context, $1, $2) . $3 }gexms;
265  
266 193 $content =~ s{($TEXTAREA) (.*?) ($END_TEXTAREA) }
267 19 { $1 . _fill_textarea($context, $1, $2) . $3 }gexms;
268  
269 193 return $content;
270 }
271  
272  
273 sub _fill_input{
274 178 ▶ my($context, $tag) = @_;
Subroutine _fill_input: called CC=10 SCAR=23.0
275  
276 ### $tag
277  
278 178 ▶ my $type = _get_type($tag) || 'text';
Condition100%2/2
_get_type($tag) || 'text'
l!l
9187
MC/DC100%1/1
_get_type($tag) || 'text'
_get_type($tag)
Truth table100%2/2
A
A= _get_type($tag)
Aresult
11
00
279 178 ▶ if($context->{ignore_types}{ $type }){
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"ignore_types"}{$type}) 9 169 178
280 9 return $tag;
281 }
282  
283 169 ▶ my $values_ref = $context->_get_param( _get_name($tag) )
Branch: 1 branch
BranchTrueFalseTotal
my $values_ref = $context->_get_param(_get_name($tag)) or return $tag 21 148 169
284 or return $tag;
285  
286 148 ▶ if($type eq 'checkbox' or $type eq 'radio'){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'checkbox' or $type eq 'radio') { } 44 104 148
Condition100%3/3
$type eq "checkbox" or $type eq "radio"
l!l&&r!l&&!r
2915104
MC/DC100%2/2
$type eq "checkbox" or $type eq "radio"
$type eq "checkbox" $type eq "radio"
Truth table100%3/3
A or B
A= $type eq "checkbox"B= $type eq "radio"
ABresult
1X1
011
000
287 44 my $value = _get_value($tag);
288  
289 44 ▶ if(not defined $value){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 7 37 44
290 7 $value = 'on';
291 }
292 else{
293 37 $value = $context->{decode_entity}->($value);
294 }
295  
296 44 ▶ if(grep { $value eq $_ } @{$values_ref}){
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$value eq $_;} @{$values_ref;}) { } 24 20 44
297 24 ▶ $tag =~ /$CHECKED/xms
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /$CHECKED/msx or $tag =~ s[$SPACE* (/?) > \z][ checked="checked" $1>]msx 19 5 24
298 or $tag =~ s{$SPACE* (/?) > \z}
299 { checked="checked" $1>}xms;
300 }
301 else{
302 20 $tag =~ s/$SPACE+$CHECKED//gxms;
303 }
304 }
305 else{
306 104 my $new_value = $context->{escape}->(shift @{$values_ref});
307  
308 104 ▶ $tag =~ s{$value = $ATTR_VALUE}{value="$new_value"}xms
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ s/$value = $ATTR_VALUE/value="$new_value"/msx or $tag =~ s[$SPACE* (/?) > \z][ value="$new_value" $1>]msx 29 75 104
309 or $tag =~ s{$SPACE* (/?) > \z}
310 { value="$new_value" $1>}xms;
311 }
312 148 return $tag;
313 }
314 sub _fill_select{
315 43 ▶ my($context, $tag, $content) = @_;
Subroutine _fill_select: called CC=3 SCAR=11.0
316  
317 43 ▶ my $values_ref = $context->_get_param( _get_name($tag) )
Branch: 1 branch
BranchTrueFalseTotal
my $values_ref = $context->_get_param(_get_name($tag)) or return $content 6 37 43
318 or return $content;
319  
320 37 ▶ if($tag !~ /$MULTIPLE/oxms){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ /$MULTIPLE/mosx) 24 13 37
321 24 $values_ref = [ shift @{ $values_ref } ]; # in select-one
322 }
323  
324 37 $content =~ s{($OPTION) (.*?) ($END_OPTION)}
325 74 { _fill_option($context, $values_ref, $1, $2) . $2 . $3 }gexms;
326 37 return $content;
327 }
328 sub _fill_option{
329 74 ▶ my($context, $values_ref, $tag, $content) = @_;
Subroutine _fill_option: called CC=4 SCAR=13.9
330  
331 74 my $value = _get_value($tag);
332 74 ▶ unless( defined $value ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 36 38 74
333 36 $value = $content;
334 36 $value =~ s{\A $SPACE+ } {}xms;
335 36 $value =~ s{ $SPACE{2,}}{ }xms;
336 36 $value =~ s{ $SPACE+ \z} {}xms;
337 }
338  
339 74 $value = $context->{decode_entity}->($value);
340  
341 ### @_
342 74 ▶ if(grep{ $value eq $_ } @{$values_ref}){
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$value eq $_;} @{$values_ref;}) { } 40 34 74
343 40 ▶ $tag =~ /$SELECTED/oxms
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /$SELECTED/mosx or $tag =~ s/ $SPACE* > \z/ selected="selected">/msx 38 2 40
344 or $tag =~ s{ $SPACE* > \z}
345 { selected="selected">}xms;
346 }
347 else{
348 34 $tag =~ s/$SPACE+$SELECTED//gxms;
349 }
350 74 return $tag;
351 }
352  
353 sub _fill_textarea{
354 19 ▶ my($context, $tag, $content) = @_;
Subroutine _fill_textarea: called CC=2 SCAR=6.9
355  
356 19 ▶ my $values_ref = $context->_get_param( _get_name($tag) )
Branch: 1 branch
BranchTrueFalseTotal
my $values_ref = $context->_get_param(_get_name($tag)) or return $content 3 16 19
357 or return $content;
358  
359 16 return $context->{escape}->(shift @{$values_ref});
360 }
361  
362 # utilities
363  
364 sub _get_param{
365 231 ▶ my($context, $name) = @_;
Subroutine _get_param: called CC=8 SCAR=20.8
366  
367 231 ▶ return if not defined $name or $context->{ignore_fields}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $context->{"ignore_fields"}{$name} 8 223 231
Condition100%3/3
not defined $name or $context->{"ignore_fields"}{$name}
l!l&&r!l&&!r
35223
MC/DC100%2/2
not defined $name or $context->{"ignore_fields"}{$name}
not defined $name $context->{"ignore_fields"}{$name}
Truth table100%3/3
not(A) or B
A= not defined $nameB= $context->{"ignore_fields"}{$name}
ABresult
1X1
011
000
368  
369 223 my $ref = $context->{params}{$name};
370  
371 223 ▶ if(not defined $ref){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 200 23 223
372 $ref = $context->{params}{$name}
373 200 = [ $context->{data}->param($name) ];
374  
375 200 ▶ if($context->{utf8}){
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"utf8"}) 7 193 200
376 7 for my $datum( @{$ref} ){
377 7 ▶ utf8::decode($datum) unless utf8::is_utf8($datum);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($datum) 5 2 7
378 }
379 }
380 }
381  
382 223 ▶ return @{$ref} ? $ref : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$ref;} ? : 201 22 223
383 }
384  
385 sub _noop{
386 64 ▶ return $_[0];
Subroutine _noop: called CC=1 SCAR=0.0
387 }
388 sub _escape_html{
389 118 ▶ my $s = shift;
Subroutine _escape_html: called CC=1 SCAR=0.0
390 # return '' unless defined $s;
391  
392 118 $s =~ s/&/&amp;/g;
393 118 $s =~ s/</&lt;/g;
394 118 $s =~ s/>/&gt;/g;
395 118 $s =~ s/"/&quot;/g; # " for poor editors
396 118 return $s;
397 }
398  
399  
400 sub _decode_entity{
401 47 ▶ my $s = shift;
Subroutine _decode_entity: called CC=5 SCAR=16.1
402  
403 47 our %entity2char;
404 47 ▶ unless(%entity2char){
Branch: 1 branch
BranchTrueFalseTotal
unless (%HTML::FillInForm::Lite::entity2char) 2 45 47
405 # load the HTML entity data
406 2 local $/ = "__END__\n";
407 2 local($@, $!);
408 2 ▶ open my $data_in, '<', __FILE__ or die $!; # should be success
Branch: 1 branch
BranchTrueFalseTotal
open my $data_in, "<", __FILE__ or die $! 0 2 2
409 2 readline $data_in; # discard the first segment
410 2 ▶ eval scalar readline($data_in) or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval scalar readline $data_in or die $@ 0 2 2
411 }
412  
413 47 ▶ $s =~ s{&(\w+);}{ $entity2char{$1} || "&$1;" }egxms;
Branch: 1 branch
BranchTrueFalseTotal
$HTML::FillInForm::Lite::entity2char{$1} or "&$1;" 1 10 11
414  
415 47 $s =~ s{&\#(\d+) ;}{ chr $1 }egxms;
416 47 $s =~ s{&\#x([0-9a-fA-F]+);}{ chr hex $1 }egxms;
417 47 return $s;
418 }
419  
420 #sub _disable{
421 # my $context = shift;
422 # my $name = shift;
423 #
424 # if($context->{disable_fields}{$name}){
425 # $_[0] =~ /$DISABLED/xmsi
426 # or $_[0] =~ s{$SPACE* /? > \z}
427 # { disabled="disabled" />}xmsi;
428 # }
429 # return;
430 #}
431  
432 sub _to_form_object{
433 216 ▶ my($ref) = @_;
Subroutine _to_form_object: called CC=8 SCAR=20.8
434  
435 216 my $wrapper;
436 my $type;
437  
438 216 ▶ if(!Scalar::Util::blessed($ref)){
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::blessed($ref)) { } 130 86 216
elsif ($ref->can('param')) { } 83 3 86
439 130 $type = ref $ref;
440 130 ▶ if($type eq 'HASH'){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 118 12 130
elsif ($type eq 'ARRAY') { } 5 7 12
elsif ($type eq 'CODE') { } 2 5 7
441 118 $wrapper = {};
442 118 @{$wrapper}{ keys %{$ref} }
443 = map{
444 153 ▶ ref($_) eq 'ARRAY' ? $_
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 23 130 153
defined $_ ? : 126 4 130
445 : defined($_) ? [$_]
446 : [ ];
447 118 } values %{$ref};
448 }
449 elsif($type eq 'ARRAY'){
450 5 $wrapper = [];
451 5 @{$wrapper} = map{ _to_form_object($_) } @{$ref};
452 }
453 elsif($type eq 'CODE'){
454 2 $wrapper = \$ref;
455 }
456 else{
457 5 Carp::croak("Cannot use '$ref' as form data");
458 }
459 }
460 elsif($ref->can('param')){ # a request object like CGI.pm
461 83 return $ref;
462 }
463 else{
464 # any object is ok
465 3 $wrapper = \$ref;
466 3 $type = 'Object';
467 }
468  
469 128 return bless $wrapper => __PACKAGE__ . q{::} . $type;
470 }
471 sub HTML::FillInForm::Lite::HASH::param{
472 117 ▶ my($hash_ref, $key) = @_;
Subroutine param: called CC=2 SCAR=6.9
473  
474 117 ▶ my $value = $hash_ref->{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $hash_ref->{$key} or return 6 111 117
475  
476 111 return @{ $value };
477 }
478  
479 sub HTML::FillInForm::Lite::ARRAY::param{
480 5 ▶ my($ary_ref, $key) = @_;
Subroutine param: called CC=1 SCAR=0.0
481  
482 5 return map{ $_->param($key) } @{$ary_ref};
483 }
484  
485 sub HTML::FillInForm::Lite::CODE::param{
486 2 ▶ my($ref_to_code_ref, $key) = @_;
Subroutine param: called CC=1 SCAR=0.0
487  
488 2 return ${$ref_to_code_ref}->($key);
489 }
490 sub HTML::FillInForm::Lite::Object::param{
491 15 ▶ my($ref_to_object, $key) = @_;
Subroutine param: called CC=4 SCAR=13.9
492 15 ▶ my $method = ${$ref_to_object}->can($key) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $method = ${$ref_to_object;}->can($key) or return 2 13 15
493 13 my(@values) = ${$ref_to_object}->$method();
494  
495 13 ▶ return @values == 1 && !defined($values[0]) ? () : @values;
Branch: 1 branch
BranchTrueFalseTotal
@values == 1 && !defined($values[0]) ? : 1 12 13
Condition100%3/3
@values == 1 and not defined $values[0]
!ll&&!rl&&r
2101
MC/DC100%2/2
@values == 1 and not defined $values[0]
@values == 1 not defined $values[0]
Truth table100%3/3
A and not(B)
A= @values == 1B= not defined $values[0]
ABresult
0X0
100
111
496 }
497  
498 18 1;
499  
500 __END__
« blib/lib/HTML/FillInForm/Lite/Compat.pm ↑ Summary Build »