Form.pm

filter: Statementstmt 93.4% 298 / 319 Branchbran 82.3% 163 / 198 Conditioncond 76.8% 53 / 69 MC/DCmcdc 56.1% 23 / 41 Subroutinesub 93.3% 28 / 30 Podpod 86.6% 13 / 15 totaltotal 86.0% 578 / 672 CC 144 SCAR 34.0
CC
144
Cov
88%
parse
62.0
find_input
25.7
param
21.1
CRAP 30.0
« Form/Input.pm ↑ Summary Form/ListInput.pm »
1 package HTML::Form;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use HTML::Form::TextInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use HTML::Form::IgnoreInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use HTML::Form::ListInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use HTML::Form::SubmitInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use HTML::Form::ImageInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use HTML::Form::FileInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use HTML::Form::KeygenInput ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 our $VERSION = '6.13';
17  
18 11 my %form_tags = map { $_ => 1 } qw(input textarea button select option);
19  
20 11 my %type2class = (
21 text => "TextInput",
22 password => "TextInput",
23 hidden => "TextInput",
24 textarea => "TextInput",
25  
26 "reset" => "IgnoreInput",
27  
28 radio => "ListInput",
29 checkbox => "ListInput",
30 option => "ListInput",
31  
32 button => "SubmitInput",
33 submit => "SubmitInput",
34 image => "ImageInput",
35 file => "FileInput",
36  
37 keygen => "KeygenInput",
38 );
39  
40 # The new HTML5 input types
41 %type2class = (
42 %type2class,
43 11 map { $_ => 'TextInput' } qw(
44 tel search url email
45 datetime date month week time datetime-local
46 number range color
47 )
48 );
49  
50 # ABSTRACT: Class that represents an HTML form element
51  
52 35 ▶sub parse {
Subroutine parse: called CC=60 SCAR=41.3
Pod documented
53 35 my $class = shift;
54 35 my $html = shift;
55 35 ▶ unshift( @_, "base" ) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 23 12 35
56 35 my %opt = @_;
57  
58 35 require HTML::TokeParser;
59 35 ▶ my $p = HTML::TokeParser->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $html ? : 3 32 35
60 ref($html) ? $html->decoded_content( ref => 1 ) : \$html );
61 35 ▶ Carp::croak "Failed to create HTML::TokeParser object" unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 35 35
62  
63 35 my $base_uri = delete $opt{base};
64 35 my $charset = delete $opt{charset};
65 35 my $strict = delete $opt{strict};
66 35 my $verbose = delete $opt{verbose};
67  
68 35 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 1 34 35
69 Carp::carp("Unrecognized option $_ in HTML::Form->parse")
70 1 for sort keys %opt;
71 }
72  
73 35 ▶ unless ( defined $base_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base_uri) 3 32 35
74 3 ▶ if ( ref($html) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html) { } 3 0 3
75 3 $base_uri = $html->base;
76 }
77 else {
78 0 Carp::croak("HTML::Form::parse: No \$base_uri provided");
79 }
80 }
81 35 ▶ unless ( defined $charset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $charset) 35 0 35
82 35 ▶ if ( ref($html) and $html->can("content_charset") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html and $html->can("content_charset")) 2 33 35
Condition100%3/3
ref $html and $html->can("content_charset")
!ll&&!rl&&r
3212
MC/DC100%2/2
ref $html and $html->can("content_charset")
ref $html $html->can("content_charset")
Truth table100%3/3
A and B
A= ref $htmlB= $html->can("content_charset")
ABresult
0X0
100
111
83 2 $charset = $html->content_charset;
84 }
85 35 ▶ unless ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($charset) 34 1 35
86 34 $charset = "UTF-8";
87 }
88 }
89  
90 35 my @forms;
91 my $f; # current form
92  
93 35 my %openselect; # index to the open instance of a select
94  
95 35 ▶ while ( my $t = $p->get_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $p->get_tag) 60 35 95
96 60 my ( $tag, $attr ) = @$t;
97 60 ▶ if ( $tag eq "form" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'form') { } 38 22 60
elsif ($form_tags{$tag}) { } 0 22 22
98 38 my $action = delete $attr->{'action'};
99 38 ▶ $action = "" unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 25 13 38
100 38 $action = URI->new_abs( $action, $base_uri );
101 $f = $class->new(
102 $attr->{'method'},
103 $action,
104 38 $attr->{'enctype'}
105 );
106 $f->accept_charset( $attr->{'accept-charset'} )
107 38 ▶ if $attr->{'accept-charset'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"accept-charset"} 1 37 38
108 38 $f->{default_charset} = $charset;
109 38 $f->{attr} = $attr;
110 38 ▶ $f->strict(1) if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 5 33 38
111 38 %openselect = ();
112 38 push( @forms, $f );
113 38 my ( %labels, $current_label );
114 38 ▶ while ( my $t = $p->get_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $p->get_tag) 208 2 210
115 208 my ( $tag, $attr ) = @$t;
116 208 ▶ last if $tag eq "/form";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/form" 36 172 208
117  
118 172 ▶ if ( $tag ne 'textarea' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne "textarea") 169 3 172
119  
120 # if we are inside a label tag, then keep
121 # appending any text to the current label
122 169 ▶ if ( defined $current_label ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_label) 13 156 169
123 $current_label = join " ",
124 13 ▶ grep { defined and length } $current_label,
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
01610
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
125 $p->get_phrase;
126 }
127 }
128  
129 172 ▶ if ( $tag eq "input" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "input") 66 106 172
130 $attr->{value_name}
131 = exists $attr->{id} && exists $labels{ $attr->{id} }
132 ? $labels{ $attr->{id} }
133 66 ▶ : defined $current_label ? $current_label
Branch: 2 branches
BranchTrueFalseTotal
exists $attr->{'id'} && exists $labels{$attr->{'id'}} ? : 1 65 66
defined $current_label ? : 5 60 65
Condition100%3/3
exists $attr->{"id"} and exists $labels{$attr->{"id"}}
!ll&&!rl&&r
5781
MC/DC100%2/2
exists $attr->{"id"} and exists $labels{$attr->{"id"}}
exists $attr->{"id"} exists $labels{$attr->{"id"}}
Truth table100%3/3
A and B
A= exists $attr->{"id"}B= exists $labels{$attr->{"id"}}
ABresult
0X0
100
111
134 : $p->get_phrase;
135 }
136  
137 172 ▶ if ( $tag eq "label" ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($tag eq 'label') { } 7 165 172
elsif ($tag eq '/label') { } 7 158 165
elsif ($tag eq 'input') { } 66 92 158
elsif ($tag eq 'button') { } 2 90 92
elsif ($tag eq 'textarea') { } 3 87 90
elsif ($tag eq 'select') { } 29 58 87
elsif ($tag eq 'keygen') { } 1 57 58
138 7 $current_label = $p->get_phrase;
139 $labels{ $attr->{for} } = $current_label
140 7 ▶ if exists $attr->{for};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{"for"} 1 6 7
141 }
142 elsif ( $tag eq "/label" ) {
143 7 $current_label = undef;
144 }
145 elsif ( $tag eq "input" ) {
146 66 ▶ my $type = delete $attr->{type} || "text";
Condition100%2/2
delete $attr->{'type'} || 'text'
l!l
5313
MC/DC100%1/1
delete $attr->{'type'} || 'text'
delete $attr->{'type'}
Truth table100%2/2
A
A= delete $attr->{'type'}
Aresult
11
00
147 66 $f->push_input( $type, $attr, $verbose );
148 }
149 elsif ( $tag eq "button" ) {
150 2 ▶ my $type = delete $attr->{type} || "submit";
Condition50%1/2
delete $attr->{'type'} || 'submit'
l!l
20
MC/DC0%0/1
delete $attr->{'type'} || 'submit'
delete $attr->{'type'}
Truth table50%1/2
A
A= delete $attr->{'type'}
Aresult
11
00
151 2 $f->push_input( $type, $attr, $verbose );
152 }
153 elsif ( $tag eq "textarea" ) {
154 $attr->{textarea_value} = $attr->{value}
155 3 ▶ if exists $attr->{value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{"value"} 0 3 3
156 3 my $text = $p->get_text("/textarea");
157 3 $attr->{value} = $text;
158 3 $f->push_input( "textarea", $attr, $verbose );
159 }
160 elsif ( $tag eq "select" ) {
161  
162 # rename attributes reserved to come for the option tag
163 29 for ( "value", "value_name" ) {
164 $attr->{"select_$_"} = delete $attr->{$_}
165 58 ▶ if exists $attr->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{$_} 1 57 58
166 }
167  
168 # count this new select option separately
169 29 my $name = $attr->{name};
170 29 ▶ $name = "" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4 25 29
171 29 $openselect{$name}++;
172  
173 29 ▶ while ( $t = $p->get_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t = $p->get_tag) 126 0 126
174 126 my $tag = shift @$t;
175 126 ▶ last if $tag eq "/select";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/select" 25 101 126
176 101 ▶ next if $tag =~ m,/?optgroup,;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ m[/?optgroup] 0 101 101
177 101 ▶ next if $tag eq "/option";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/option" 25 76 101
178 76 ▶ if ( $tag eq "option" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'option') { } 71 5 76
179 71 my %a = %{ $t->[0] };
180  
181 # rename keys so they don't clash with %attr
182 71 for ( keys %a ) {
183 56 ▶ next if $_ eq "value";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "value" 31 25 56
184 25 $a{"option_$_"} = delete $a{$_};
185 }
186 71 ▶ while ( my ( $k, $v ) = each %$attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 123 71 194
187 123 $a{$k} = $v;
188 }
189 71 $a{value_name} = $p->get_trimmed_text;
190 $a{value} = delete $a{value_name}
191 71 ▶ unless defined $a{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"value"} 40 31 71
192 71 $a{idx} = $openselect{$name};
193 71 $f->push_input( "option", \%a, $verbose );
194 }
195 else {
196 5 ▶ warn("Bad <select> tag '$tag' in $base_uri\n")
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
197 if $verbose;
198 5 ▶ if ( $tag eq "/form"
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen") 4 1 5
Condition75%9/12
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input" or $tag eq "textarea"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input"
l!l&&r!l&&!r
221
MC/DC40%2/5
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen"
$tag eq "/form" $tag eq "input" $tag eq "textarea" $tag eq "select" $tag eq "keygen"
Truth table50%3/6
A or B or C or D or E
A= $tag eq "/form"B= $tag eq "input"C= $tag eq "textarea"D= $tag eq "select"E= $tag eq "keygen"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
199 || $tag eq "input"
200 || $tag eq "textarea"
201 || $tag eq "select"
202 || $tag eq "keygen" ) {
203  
204 # MSIE implicitly terminates the <select> here, so we
205 # try to do the same. Actually the MSIE behaviour
206 # appears really strange: <input> and <textarea>
207 # do implicitly close, but not <select>, <keygen> or
208 # </form>.
209 4 ▶ my $type = ( $tag =~ s,^/,, ) ? "E" : "S";
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ s[^/][] ? : 2 2 4
210 4 $p->unget_token( [ $type, $tag, @$t ] );
211 4 last;
212 }
213 }
214 }
215 }
216 elsif ( $tag eq "keygen" ) {
217 1 $f->push_input( "keygen", $attr, $verbose );
218 }
219 }
220 }
221 elsif ( $form_tags{$tag} ) {
222 0 ▶ warn("<$tag> outside <form> in $base_uri\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
223 }
224 }
225 35 for (@forms) {
226 38 $_->fixup;
227 }
228  
229 35 ▶ wantarray ? @forms : $forms[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 30 35
230 }
231  
232 49 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
233 49 my $class = shift;
234 49 my $self = bless {}, $class;
235 49 ▶ $self->{method} = uc( shift || "GET" );
Condition100%2/2
shift() || 'GET'
l!l
2029
MC/DC100%1/1
shift() || 'GET'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
236 49 ▶ $self->{action} = shift || Carp::croak("No action defined");
Condition50%1/2
shift() || Carp::croak('No action defined')
l!l
490
MC/DC0%0/1
shift() || Carp::croak('No action defined')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
237 49 ▶ $self->{enctype} = lc( shift || "application/x-www-form-urlencoded" );
Condition100%2/2
shift() || 'application/x-www-form-urlencoded'
l!l
1435
MC/DC100%1/1
shift() || 'application/x-www-form-urlencoded'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
238 49 $self->{accept_charset} = "UNKNOWN";
239 49 $self->{default_charset} = "UTF-8";
240 49 $self->{inputs} = [@_];
241 49 $self;
242 }
243  
244 154 ▶sub push_input {
Subroutine push_input: called CC=7 SCAR=19.5
Pod documented
245 154 my ( $self, $type, $attr, $verbose ) = @_;
246 154 $type = lc $type;
247 154 my $class = $type2class{$type};
248 154 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 1 153 154
249 1 ▶ Carp::carp("Unknown input type '$type'") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 1 0 1
250 1 $class = "TextInput";
251 }
252 154 $class = "HTML::Form::$class";
253 154 my @extra;
254 154 ▶ push( @extra, readonly => 1 ) if $type eq "hidden";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hidden" 7 147 154
255 154 ▶ push( @extra, strict => 1 ) if $self->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 43 111 154
256 154 ▶ if ( $type eq "file" && exists $attr->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file" and exists $attr->{"value"}) 2 152 154
Condition100%3/3
$type eq "file" and exists $attr->{"value"}
!ll&&!rl&&r
141112
MC/DC100%2/2
$type eq "file" and exists $attr->{"value"}
$type eq "file" exists $attr->{"value"}
Truth table100%3/3
A and B
A= $type eq "file"B= exists $attr->{"value"}
ABresult
0X0
100
111
257  
258 # it's not safe to trust the value set by the server
259 # the user always needs to explicitly set the names of files to upload
260 2 $attr->{orig_value} = delete $attr->{value};
261 }
262 154 delete $attr->{type}; # don't confuse the type argument
263 154 my $input = $class->new( type => $type, %$attr, @extra );
264 154 $input->add_to_form($self);
265 }
266  
267 11 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
268 # Set up some accessors
269 11 for my $m (qw(method action enctype accept_charset)) {
270 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
271 44 ▶ *{$m} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
272 86 my $self = shift;
273 86 my $old = $self->{$m};
274 86 ▶ $self->{$m} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 79 86
275 86 $old;
276 44 };
277 }
278 11 *uri = \&action; # alias
279 }
280  
281 2 ▶sub attr {
Subroutine attr: called CC=3 SCAR=11.2
Pod documented
282 2 my $self = shift;
283 2 my $name = shift;
284 2 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 2 2
285  
286 2 my $old = $self->{attr}{$name};
287 2 ▶ $self->{attr}{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
288 2 return $old;
289 }
290  
291 6 ▶sub strict {
Subroutine strict: called CC=3 SCAR=11.0
Pod documented
292 6 my $self = shift;
293 6 my $old = $self->{strict};
294 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 0 6
295 6 $self->{strict} = shift;
296 6 for my $input ( @{ $self->{inputs} } ) {
297 7 $input->strict( $self->{strict} );
298 }
299 }
300 6 return $old;
301 }
302  
303 45 ▶sub inputs {
Subroutine inputs: called CC=1 SCAR=0.0
Pod documented
304 45 my $self = shift;
305 45 @{ $self->{'inputs'} };
306 }
307  
308 179 ▶sub find_input {
Subroutine find_input: called CC=25 SCAR=32.5
Pod documented
309 179 my ( $self, $selector, $type, $no ) = @_;
310 179 ▶ Carp::croak "Invalid index $no"
Branch: 1 branch
BranchTrueFalseTotal
if defined $no and $no < 1 2 177 179
Condition100%3/3
defined $no and $no < 1
!ll&&!rl&&r
104732
MC/DC100%2/2
defined $no and $no < 1
defined $no $no < 1
Truth table100%3/3
A and B
A= defined $noB= $no < 1
ABresult
0X0
100
111
311 if defined $no && $no < 1;
312 177 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 173 177
313 4 ▶ warn "find_input called in list context with index specified\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $no 1 3 4
314 if defined $no;
315 4 my @res;
316 my $c;
317 4 for ( @{ $self->{'inputs'} } ) {
318 11 ▶ if ( defined($selector) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) 9 2 11
319  
320 # an input that explicitly has no name
321 9 ▶ if ( ref($selector) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selector eq 'SCALAR') { } 4 5 9
322 next
323 4 ▶ if !defined($$selector) && $_->{name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$selector and $_->{"name"} 1 3 4
Condition66%2/3
not defined $$selector and $_->{"name"}
!ll&&!rl&&r
031
MC/DC50%1/2
not defined $$selector and $_->{"name"}
not defined $$selector $_->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $$selectorB= $_->{"name"}
ABresult
0X0
100
111
324 }
325  
326 # an input that does not fit this selector
327 else {
328 next
329 5 ▶ if !$_->selected($selector);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->selected($selector) 3 2 5
330 }
331 }
332 7 ▶ next if $type && $type ne $_->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne $_->{"type"} 0 7 7
Condition66%2/3
$type and $type ne $_->{"type"}
!ll&&!rl&&r
520
MC/DC0%0/2
$type and $type ne $_->{"type"}
$type $type ne $_->{"type"}
Truth table66%2/3
A and B
A= $typeB= $type ne $_->{"type"}
ABresult
0X0
100
111
333 7 $c++;
334 7 ▶ next if $no && $no != $c;
Branch: 1 branch
BranchTrueFalseTotal
if $no and $no != $c 0 7 7
Condition33%1/3
$no and $no != $c
!ll&&!rl&&r
700
MC/DC0%0/2
$no and $no != $c
$no $no != $c
Truth table33%1/3
A and B
A= $noB= $no != $c
ABresult
0X0
100
111
335 7 push( @res, $_ );
336 }
337 4 return @res;
338  
339 }
340 else {
341 173 ▶ $no ||= 1;
Condition100%2/2
$no ||= 1
l!l
72101
MC/DC100%1/1
$no ||= 1
$no
Truth table100%2/2
A
A= $no
Aresult
11
00
342 173 for ( @{ $self->{'inputs'} } ) {
343 594 ▶ if ( defined($selector) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) 594 0 594
344  
345 # an input that explicitly has no name
346 594 ▶ if ( ref($selector) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selector eq 'SCALAR') { } 19 575 594
347 next
348 19 ▶ if !defined($$selector) && $_->{name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$selector and $_->{"name"} 5 14 19
Condition66%2/3
not defined $$selector and $_->{"name"}
!ll&&!rl&&r
0145
MC/DC50%1/2
not defined $$selector and $_->{"name"}
not defined $$selector $_->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $$selectorB= $_->{"name"}
ABresult
0X0
100
111
349 }
350  
351 # an input that does not fit this selector
352 else {
353 next
354 575 ▶ if !$_->selected($selector);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->selected($selector) 419 156 575
355 }
356 }
357 170 ▶ next if $type && $type ne $_->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne $_->{"type"} 8 162 170
Condition100%3/3
$type and $type ne $_->{"type"}
!ll&&!rl&&r
119438
MC/DC100%2/2
$type and $type ne $_->{"type"}
$type $type ne $_->{"type"}
Truth table100%3/3
A and B
A= $typeB= $type ne $_->{"type"}
ABresult
0X0
100
111
358 162 ▶ next if --$no;
Branch: 1 branch
BranchTrueFalseTotal
if --$no 22 140 162
359 140 return $_;
360 }
361 33 return undef;
362 }
363 }
364  
365 38 ▶sub fixup {
Subroutine fixup: called CC=2 SCAR=6.9
Pod undocumented
366 38 my $self = shift;
367 38 for ( @{ $self->{'inputs'} } ) {
368 105 $_->fixup;
369 }
370 }
371  
372 41 ▶sub value {
Subroutine value: called CC=4 SCAR=18.6
Pod documented
373 41 my $self = shift;
374 41 my $key = shift;
375 41 my $input = $self->find_input($key);
376 41 ▶ unless ($input) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input) 0 41 41
377 0 ▶ Carp::croak("No such field '$key'") if $self->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 0 0 0
378 0 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
379 0 $input = $self->push_input( "text", { name => $key, value => "" } );
380 }
381 41 local $Carp::CarpLevel = 1;
382 41 $input->value(@_);
383 }
384  
385 32 ▶sub param {
Subroutine param: called CC=21 SCAR=30.5
Pod documented
386 32 my $self = shift;
387 32 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 30 2 32
388 30 my $name = shift;
389 30 my @inputs;
390 30 for ( $self->inputs ) {
391 186 my $n = $_->name;
392 186 ▶ next if !defined($n) || $n ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $n ne $name 135 51 186
Condition66%2/3
not defined $n or $n ne $name
l!l&&r!l&&!r
013551
MC/DC50%1/2
not defined $n or $n ne $name
not defined $n $n ne $name
Truth table66%2/3
not(A) or B
A= not defined $nB= $n ne $name
ABresult
1X1
011
000
393 51 push( @inputs, $_ );
394 }
395  
396 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 22 30
397  
398 # set
399 8 ▶ Carp::croak "No '$name' parameter exists" unless @inputs;
Branch: 1 branch
BranchTrueFalseTotal
unless @inputs 0 8 8
400 8 my @v = @_;
401 8 ▶ @v = @{ $v[0] } if @v == 1 && ref( $v[0] );
Branch: 1 branch
BranchTrueFalseTotal
if @v == 1 and ref $v[0] 3 5 8
Condition100%3/3
@v == 1 and ref $v[0]
!ll&&!rl&&r
233
MC/DC100%2/2
@v == 1 and ref $v[0]
@v == 1 ref $v[0]
Truth table100%3/3
A and B
A= @v == 1B= ref $v[0]
ABresult
0X0
100
111
402 8 ▶ while (@v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 9 6 15
403 9 my $v = shift @v;
404 9 my $err;
405 9 for my $i ( 0 .. @inputs - 1 ) {
406 16 eval { $inputs[$i]->value($v); };
407 16 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 7 9 16
408 7 undef($err);
409 7 splice( @inputs, $i, 1 );
410 7 last;
411 }
412 9 ▶ $err ||= $@;
Condition66%2/3
$err ||= $@
l!l&&r!l&&!r
270
MC/DC0%0/2
$err ||= $@
$err $@
Truth table66%2/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
413 }
414 9 ▶ Carp::croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 7 9
415 }
416  
417 # the rest of the input should be cleared
418 6 for (@inputs) {
419 6 $_->value(undef);
420 }
421 }
422 else {
423 # get
424 22 my @v;
425 22 for (@inputs) {
426 35 ▶ if ( defined( my $v = $_->value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $_->value)) 22 13 35
427 22 push( @v, $v );
428 }
429 }
430 22 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 7 22
431 }
432 }
433 else {
434 # list parameter names
435 2 my @n;
436 my %seen;
437 2 for ( $self->inputs ) {
438 14 my $n = $_->name;
439 14 ▶ next if !defined($n) || $seen{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $seen{$n}++ 6 8 14
Condition66%2/3
not defined $n or $seen{$n}++
l!l&&r!l&&!r
068
MC/DC50%1/2
not defined $n or $seen{$n}++
not defined $n $seen{$n}++
Truth table66%2/3
not(A) or B
A= not defined $nB= $seen{$n}++
ABresult
1X1
011
000
440 8 push( @n, $n );
441 }
442 2 return @n;
443 }
444 }
445  
446 0 ▶sub try_others {
Subroutine try_others: not called CC=4 SCAR=30.0
Pod documented
447 0 my ( $self, $cb ) = @_;
448 0 my @try;
449 0 for ( @{ $self->{'inputs'} } ) {
450 0 my @not_tried_yet = $_->other_possible_values;
451 0 ▶ next unless @not_tried_yet;
Branch: 1 branch
BranchTrueFalseTotal
unless @not_tried_yet 0 0 0
452 0 push( @try, [ \@not_tried_yet, $_ ] );
453 }
454 0 ▶ return unless @try;
Branch: 1 branch
BranchTrueFalseTotal
unless @try 0 0 0
455 0 $self->_try( $cb, \@try, 0 );
456 }
457  
458 0 ▶sub _try {
Subroutine _try: not called CC=3 SCAR=24.8
459 0 my ( $self, $cb, $try, $i ) = @_;
460 0 for ( @{ $try->[$i][0] } ) {
461 0 $try->[$i][1]->value($_);
462 0 &$cb($self);
463 0 ▶ $self->_try( $cb, $try, $i + 1 ) if $i + 1 < @$try;
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 < @$try 0 0 0
464 }
465 }
466  
467 42 ▶sub make_request {
Subroutine make_request: called CC=6 SCAR=18.0
Pod documented
468 42 my $self = shift;
469 42 my $method = uc $self->{'method'};
470 42 my $uri = $self->{'action'};
471 42 my $enctype = $self->{'enctype'};
472 42 my @form = $self->form;
473  
474 my $charset
475 = $self->accept_charset eq "UNKNOWN"
476 ? $self->{default_charset}
477 42 ▶ : $self->accept_charset;
Branch: 1 branch
BranchTrueFalseTotal
$self->accept_charset eq 'UNKNOWN' ? : 38 4 42
478 42 foreach my $fi (@form) {
479 142 ▶ $fi = Encode::encode( $charset, $fi ) unless ref($fi);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fi 130 12 142
480 }
481  
482 42 ▶ if ( $method eq "GET" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 21 21 42
elsif ($method eq 'POST') { } 21 0 21
483 21 require HTTP::Request;
484 21 $uri = URI->new( $uri, "http" );
485 21 $uri->query_form(@form);
486 21 return HTTP::Request->new( GET => $uri );
487 }
488 elsif ( $method eq "POST" ) {
489 21 require HTTP::Request::Common;
490 21 return HTTP::Request::Common::POST(
491 $uri, \@form,
492 Content_Type => $enctype
493 );
494 }
495 else {
496 0 Carp::croak("Unknown method '$method'");
497 }
498 }
499  
500 31 ▶sub click {
Subroutine click: called CC=8 SCAR=21.5
Pod documented
501 31 my $self = shift;
502 31 my $name;
503 31 ▶ $name = shift if ( @_ % 2 ) == 1; # odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 0 31 31
504  
505 # try to find first submit button to activate
506 31 for ( @{ $self->{'inputs'} } ) {
507 59 ▶ next unless $_->can("click");
Branch: 1 branch
BranchTrueFalseTotal
unless $_->can("click") 52 7 59
508 7 ▶ next if $name && !$_->selected($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name and not $_->selected($name) 0 7 7
Condition33%1/3
$name and not $_->selected($name)
!ll&&!rl&&r
700
MC/DC0%0/2
$name and not $_->selected($name)
$name not $_->selected($name)
Truth table33%1/3
A and not(B)
A= $nameB= not $_->selected($name)
ABresult
0X0
100
111
509 7 ▶ next if $_->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $_->disabled 1 6 7
510 6 return $_->click( $self, @_ );
511 }
512 25 ▶ Carp::croak("No clickable input with name $name") if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 25 25
513 25 $self->make_request;
514 }
515  
516 44 ▶sub form {
Subroutine form: called CC=1 SCAR=0.0
Pod documented
517 44 my $self = shift;
518 44 map { $_->form_name_value($self) } @{ $self->{'inputs'} };
519 }
520  
521 2 ▶sub dump {
Subroutine dump: called CC=5 SCAR=16.2
Pod documented
522 2 my $self = shift;
523 2 my $method = $self->{'method'};
524 2 my $uri = $self->{'action'};
525 2 my $enctype = $self->{'enctype'};
526 2 my $dump = "$method $uri";
527 2 ▶ $dump .= " ($enctype)"
Branch: 1 branch
BranchTrueFalseTotal
if $enctype ne "application/x-www-form-urlencoded" 0 2 2
528 if $enctype ne "application/x-www-form-urlencoded";
529 $dump .= " [$self->{attr}{name}]"
530 2 ▶ if exists $self->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attr"}{"name"} 1 1 2
531 2 $dump .= "\n";
532  
533 2 for ( $self->inputs ) {
534 1 $dump .= " " . $_->dump . "\n";
535 }
536 2 ▶ print STDERR $dump unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2 2
537 2 $dump;
538 }
539  
540 11 1;
541  
542 __END__
« Form/Input.pm ↑ Summary Form/ListInput.pm »