StickyForm.pm

filter: Statementstmt 97.5% 234 / 240 Branchbran 93.8% 107 / 114 Conditioncond 76.6% 23 / 30 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 24 / 24 Podpod 100% 18 / 18 totaltotal 93.8% 415 / 442 CC 74 SCAR 22.2
CC
74
Cov
95%
checkbox_group
22.0
select
10.1
_select_options
9.0
CRAP 9.2
« StickyForms.pm ↑ Summary StickyForm/RequestHash.pm »
1  
2 =head1 NAME
3  
4 HTML::StickyForm - Lightweight general-purpose HTML form generation, with sticky values
5  
6 =head1 SYNOPSIS
7  
8 # mod_perl example
9  
10 use HTML::StickyForm;
11 use Apache::Request;
12  
13 sub handler{
14 my($r)=@_;
15 $r=Apache::Request->new($r);
16 my $f=HTML::StickyForm->new($r);
17  
18 $r->send_http_header;
19 print
20 '<html><body>',
21 $form->form_start,
22  
23 "Text field:",
24 $f->text(name => 'field1', size => 40, default => 'default value'),
25  
26 "<br />Text area:",
27 $f->textarea(name => 'field2', cols => 60, rows => 5, default => 'stuff'),
28  
29 "<br />Single radio button:",
30 $f->radio(name => 'field3', value => 'xyz', checked => 1),
31  
32 "<br />Radio buttons:",
33 $f->radio_group(name => 'field4', values => [1,2,3],
34 labels => { 1=>'one', 2=>'two', 3=>'three' }, default => 2),
35  
36 "<br />Single checkbox:",
37 $f->checkbox(name => 'field5', value => 'xyz', checked => 1),
38  
39 "<br />Checkbox group:",
40 $f->checkbox_group(name => 'field6', values => [4,5,6],
41 labels => { 4=>'four', 5=>'five', 6=>'six' }, default => [5,6]),
42  
43 "<br />Password field:",
44 $f->password(name => 'field7', size => 20),
45  
46 '<br />",
47 $f->submit(value => ' Hit me! '),
48  
49 $f->form_end,
50 '</body></html>',
51 ;
52 return OK;
53 }
54  
55 =head1 DESCRIPTION
56  
57 This module provides a simple interface for generating HTML form
58 elements, with default values chosen from the previous form submission. This
59 module was written with mod_perl (L<Apache::Request>) in mind, but works
60 equally well with CGI.pm, including the new 3.x version, or any other module
61 which implements a param() method, or even completely standalone.
62  
63 The module does not provide methods for generating all possible HTML elements,
64 only those which are used in form construction.
65 In addition, this module's interface is much less flexible than CGI.pm's; all
66 routines work only as methods, and there is only one way of passing parameters
67 to each method. This was done for two reasons: to keep the API simple and
68 consistent, and to keep the code size down to a minimum.
69  
70 =cut
71  
72  
73 package HTML::StickyForm;
74 BEGIN {
75 18 ▶ $HTML::StickyForm::VERSION = '0.07_02';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 }
77 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 =head1 CLASS METHODS
81  
82 =over
83  
84 =item new([REQUEST])
85  
86 Creates a new form generation object. The single argument can be:
87  
88 =over
89  
90 =item *
91  
92 any object which responds to a C<param> method in the same way that L<CGI> and
93 L<Apache::Request> objects do. That is, with no arguments, the names of the
94 parameters are returned as a list. With a single argument, the value(s) of the
95 supplied parameter is/are returned; in scalar context the first value,
96 and in list context all values.
97  
98 =item *
99  
100 a plain arrayref. This will be used to construct an
101 L<HTML::StickyForm::RequestHash> object, which responds as described above.
102 The array will be passed directly to the RequestHash constructor, so both
103 methods for specifying multiple values are allowed.
104  
105 =item *
106  
107 a plain hashref. This will be used to construct an
108 L<HTML::StickyForm::RequestHash> object. Multiple values must be represented
109 as arrayref values.
110  
111 =item *
112  
113 a false value. This will be used to construct an
114 L<HTML::StickyForm::RequestHash> object with no parameters set.
115  
116 =back
117  
118 The constructor dies if passed an unrecognised request object.
119  
120 If an appropriate object is supplied, parameters will be fetched from the
121 object on an as needed basis, which means that changes made to the request
122 object after the form object is constructed may affect the default values
123 used in generated form elements. However, once constructed, the form object's
124 sticky status does not get automatically updated, so if changes made to the
125 request object need to affect the form object's sticky status, set_sticky()
126 must be called between request object modification and form generation.
127  
128 In contrast, L<HTML::StickyForm::RequestHash> objects created as part of form
129 object construction use copies of the parameters from the supplied hashref or
130 arrayref. This means that the changes made to the original data do not affect
131 the request object, so have absolutely no effect of the behaviour of the
132 form object.
133  
134 =cut
135  
136 sub new{
137 25 ▶ my($class,$req)=@_;
Subroutine new: called CC=8 SCAR=21.9
Pod documented
138  
139 # Identify the type of request
140 25 my $type;
141 25 ▶ if(!$req){
Branch: 4 branches
BranchTrueFalseTotal
if (not $req) { } 13 12 25
elsif (eval { do { local $SIG{'__DIE__'}; $req->can('param') } }) { } 11 1 12
elsif (ref $req eq 'HASH') { } 1 0 1
elsif (ref $req eq 'ARRAY') { } 0 0 0
142 13 $type='hash';
143 13 $req={};
144 12 }elsif(eval{ local $SIG{__DIE__}; $req->can('param'); }){
145 11 $type='object';
146 }elsif(ref($req) eq 'HASH'){
147 1 $type='hash';
148 }elsif(ref($req) eq 'ARRAY'){
149 0 $type='array';
150 }else{
151 0 require Carp;
152 0 Carp::croak(
153 "Unrecognised request passed to HTML::StickyForm constructor ($req)");
154 }
155 25 ▶ if($type eq 'hash' || $type eq 'array'){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hash" or $type eq "array") 14 11 25
Condition66%2/3
$type eq "hash" or $type eq "array"
l!l&&r!l&&!r
14011
MC/DC50%1/2
$type eq "hash" or $type eq "array"
$type eq "hash" $type eq "array"
Truth table66%2/3
A or B
A= $type eq "hash"B= $type eq "array"
ABresult
1X1
011
000
156 14 require HTML::StickyForm::RequestHash;
157 14 ▶ $req=HTML::StickyForm::RequestHash->new($type eq 'hash' ? %$req : @$req);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'hash' ? : 14 0 14
158 }
159  
160 25 my $self=bless {
161 req => $req,
162 values_as_labels => 0,
163 well_formed => ' /',
164 },$class;
165  
166 # Count submitted fields
167 25 $self->set_sticky;
168  
169 25 $self;
170 }
171  
172 =back
173  
174 =head1 METHODS
175  
176 =head2 Configuration methods
177  
178 =over
179  
180 =item set_sticky([BOOL])
181  
182 With no arguments, the request object's parameters are counted, and the form
183 object is made sticky if one or more parameters are present, non-sticky
184 otherwise. If an argument is given, its value as a boolean determines whether
185 the form object will be sticky or not. In both cases, the return value will be
186 the new value of the sticky flag.
187  
188 A non-sticky form object always uses the values supplied to methods when
189 constructing HTML elements, whereas a sticky form object will use the values
190 from the request.
191  
192 This method is called by the constructor when the form object is created, so it
193 is not usually necessary to call it explicitly. However, it may be necessary to
194 call this method if parameters are set with the C<param()> method after the
195 form object is created.
196  
197 =cut
198  
199 sub set_sticky{
200 30 ▶ my $self=shift;
Subroutine set_sticky: called CC=2 SCAR=6.9
Pod documented
201 30 ▶ return $self->{params}=!!$_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 28 30
202  
203 28 $self->{params}=!!(()=$self->{req}->param);
204 }
205  
206 =item get_sticky()
207  
208 Returns true if the form object is sticky.
209  
210 =cut
211  
212 sub get_sticky{
213 7 ▶ my($self)=@_;
Subroutine get_sticky: called CC=1 SCAR=0.0
Pod documented
214  
215 7 !!$self->{params};
216 }
217  
218 =item values_as_labels([BOOL])
219  
220 With no arguments, this method returns the C<values_as_labels> attribute,
221 which determines what should happen when a value has no label in the
222 checkbox_group(), radio_group() and select() methods. If this attribute
223 is false (the default), no labels will be automatically generated. If it is
224 true, labels will default to the corresponding value if they are not supplied
225 by the user.
226  
227 If an argument is passed, it is used to set the C<values_as_labels> attribute.
228  
229 =cut
230  
231 sub values_as_labels{
232 6 ▶ my $self=shift;
Subroutine values_as_labels: called CC=2 SCAR=6.9
Pod documented
233 6 ▶ return $self->{values_as_labels}=!!$_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
234 4 $self->{values_as_labels};
235 }
236  
237 =item well_formed([BOOL])
238  
239 With no arguments, this method return the C<well_formed> attribute, which
240 determines whether to generate well-formed XML, by including the trailing
241 slash in non-container elements.
242 If true, all generated elements will be well-formed. If false, no slashes
243 are added - which is unfortunately required by some older browsers.
244  
245 If an argument is passed, it is used to set the C<well_formed> attribute.
246  
247 =cut
248  
249 sub well_formed{
250 6 ▶ my $self=shift;
Subroutine well_formed: called CC=3 SCAR=11.0
Pod documented
251 6 ▶ return !!($self->{well_formed}=$_[0] ? ' /' : '') if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 2 4 6
$_[0] ? : 1 1 2
252 4 !!$self->{well_formed};
253 }
254  
255 =back
256  
257 =head2 HTML generation methods
258  
259 Most of these methods are specified as taking PAIRLIST arguments. This means
260 that arguments must be passed as a list of name/value pairs. For example:
261  
262 $form->text(name => 'fred',value => 'bloggs');
263  
264 This represents two arguments; "name" with a value of "fred", and "value"
265 with a value of "bloggs".
266  
267 In cases where sticky values are useful, there are two ways of specifying the
268 values, depending on whether stickiness is required for the element being
269 generated. To set sticky value defaults, use the C<default> argument.
270 Alternatively, to set values which are not affected by previous values entered
271 by the user, use the C<value> argument (or C<selected> or C<checked>, depending
272 on the type of element being generated).
273  
274 =over
275  
276 =item form_start(PAIRLIST)
277  
278 Generates a C<E<lt>formE<gt>> start tag. All arguments are interpreted
279 as attributes for the element. All names and values are HTML escaped.
280 The following arguments are treated specially:
281  
282 C<method>: Defaults to C<GET>
283  
284 =cut
285  
286 sub form_start{
287 8 ▶ my($self,$args)=&_args;
Subroutine form_start: called CC=3 SCAR=11.0
Pod documented
288 8 ▶ $args->{method}='GET' unless exists $args->{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"method"} 6 2 8
289  
290 8 my $field='<form';
291 8 ▶ while(my($name,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 12 8 20
292 12 _escape($name);
293 12 _escape($val);
294 12 $field.=qq( $name="$val");
295 }
296 8 $field.='>';
297  
298 8 $field;
299 }
300  
301 =item form_start_multipart(PAIRLIST)
302  
303 As form_start(), but the C<enctype> argument defaults to C<multipart/form-data>.
304  
305 =cut
306  
307 sub form_start_multipart{
308 2 ▶ my($self,$args)=&_args;
Subroutine form_start_multipart: called CC=2 SCAR=7.1
Pod documented
309 2 ▶ $args->{enctype}||='mutipart/form-data';
Condition50%1/2
$args->{"enctype"} ||= "mutipart/form-data"
l!l
02
MC/DC0%0/1
$args->{"enctype"} ||= "mutipart/form-data"
$args->{"enctype"}
Truth table50%1/2
A
A= $args->{"enctype"}
Aresult
11
00
310 2 $self->form_start($args);
311 }
312  
313 =item form_end()
314  
315 Generates a C<E<lt>formE<gt>> end tag.
316  
317 =cut
318  
319 sub form_end{
320 1 ▶ '</form>';
Subroutine form_end: called CC=1 SCAR=0.0
Pod documented
321 }
322  
323 =item text(PAIRLIST)
324  
325 Generates an C<E<lt>inputE<gt>> element. In general, arguments are interpreted
326 as attributes for the element. All names and values are HTML escaped. The
327 following arguments are treated specially:
328  
329 C<type>: Defaults to C<text>
330  
331 C<value>: Unconditional value. If present, causes C<default> and any sticky
332 value to be ignored.
333  
334 C<default>: Conditional value, ignored if C<value> is present. If the form is
335 sticky, the sticky value will be used for the C<value> attribute's value.
336 Otherwise, the supplied C<default> will be used.
337 A C<default> attribute is never created.
338  
339 =cut
340  
341 sub text{
342 64 ▶ my($self,$args)=&_args;
Subroutine text: called CC=5 SCAR=16.1
Pod documented
343 64 ▶ my $type=delete $args->{type} || 'text';
Condition100%2/2
delete $args->{'type'} || 'text'
l!l
3628
MC/DC100%1/1
delete $args->{'type'} || 'text'
delete $args->{'type'}
Truth table100%2/2
A
A= delete $args->{'type'}
Aresult
11
00
344 64 my $name=delete $args->{name};
345 64 my $value;
346 64 ▶ if(exists $args->{value}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'value'}) { } 16 48 64
347 16 $value=delete $args->{value};
348 16 delete $args->{default};
349 }else{
350 48 $value=delete $args->{default};
351 48 ▶ $value=$self->{req}->param($name) if $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"} 24 24 48
352 }
353  
354 64 _escape($type);
355 64 _escape($name);
356 64 _escape($value);
357  
358 64 my $field=qq(<input type="$type" name="$name" value="$value");
359 64 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 4 64 68
360 4 _escape($key);
361 4 _escape($val);
362 4 $field.=qq( $key="$val");
363 }
364  
365 64 return "$field$self->{well_formed}>";
366 }
367  
368 =item hidden(PAIRLIST)
369  
370 As text(), but produces an input element of type C<hidden>.
371  
372 =cut
373  
374 sub hidden{
375 16 ▶ my($self,$args)=&_args;
Subroutine hidden: called CC=2 SCAR=7.1
Pod documented
376 16 ▶ $args->{type}||='hidden';
Condition50%1/2
$args->{"type"} ||= "hidden"
l!l
016
MC/DC0%0/1
$args->{"type"} ||= "hidden"
$args->{"type"}
Truth table50%1/2
A
A= $args->{"type"}
Aresult
11
00
377 16 $self->text($args);
378 }
379  
380 =item password(PAIRLIST)
381  
382 As text(), but produces an input element of type C<password>.
383  
384 =cut
385  
386 sub password{
387 16 ▶ my($self,$args)=&_args;
Subroutine password: called CC=2 SCAR=7.1
Pod documented
388 16 ▶ $args->{type}||='password';
Condition50%1/2
$args->{"type"} ||= "password"
l!l
016
MC/DC0%0/1
$args->{"type"} ||= "password"
$args->{"type"}
Truth table50%1/2
A
A= $args->{"type"}
Aresult
11
00
389 16 $self->text($args);
390 }
391  
392 =item textarea(PAIRLIST)
393  
394 Generates a E<lt>textareaE<gt> container. All arguments are used directly
395 to generate attributes for the start tag, except for those listed below.
396 All values are HTML-escaped.
397  
398 C<value>: Unconditional value. If present, specifies the contents of the
399 container, and causes C<default> and any sticky value to be ignored. A
400 C<value> attribute is never created.
401  
402 C<default>: Conditional value, ignored if C<value> is present. If the form is
403 stikcy, the sticky value wil be used for the container contents. Otherwise,
404 sticky, the supplied C<default> will be used.
405 A C<default> attribute is never created.
406  
407 =cut
408  
409 sub textarea{
410 28 ▶ my($self,$args)=&_args;
Subroutine textarea: called CC=4 SCAR=13.9
Pod documented
411 28 my $name=delete $args->{name};
412 28 my $value;
413 28 ▶ if(exists $args->{value}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'value'}) { } 8 20 28
414 8 $value=delete $args->{value};
415 8 delete $args->{default};
416 }else{
417 20 $value=delete $args->{default};
418 20 ▶ $value=$self->{req}->param($name) if $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"} 10 10 20
419 }
420  
421 28 _escape($name);
422 28 _escape($value);
423  
424 28 my $field=qq(<textarea name="$name");
425 28 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 4 28 32
426 4 _escape($key);
427 4 _escape($val);
428 4 $field.=qq( $key="$val");
429 }
430  
431 28 return "$field>$value</textarea>";
432 }
433  
434 =item checkbox(PAIRLIST)
435  
436 Generates a single C<checkbox> type C<E<lt>inputE<gt>> element. All arguments
437 are used directly to generate attributes for the tag, except for those listed
438 below. All values are HTML-escaped.
439  
440 C<checked>: Unconditional status. If present, used to decide whether to include
441 a checked attribute, and causes C<default> and any sticky value to be ignored.
442  
443 C<default>: Conditional status, ignored if C<checked> is present. If the form
444 is sticky, the sticky value will be used to determine whether to include a
445 checked attribute. Otherwise, the supplied C<default> will be used.
446  
447 If the decision to include the C<checked> attribute is based on the sticky
448 value, the sticky parameter must include at least one value which is the same
449 as the supplied C<value> argument. If the decision is based on the value of
450 the C<checked> or C<default> arguments, the supplied argument need only be
451 true for the C<checked> attribute to be created.
452  
453 =cut
454  
455 sub checkbox{
456 120 ▶ my($self,$args)=&_args;
Subroutine checkbox: called CC=7 SCAR=19.5
Pod documented
457 120 ▶ my $type=delete $args->{type} || 'checkbox';
Condition100%2/2
delete $args->{'type'} || 'checkbox'
l!l
6060
MC/DC100%1/1
delete $args->{'type'} || 'checkbox'
delete $args->{'type'}
Truth table100%2/2
A
A= delete $args->{'type'}
Aresult
11
00
458 120 my $name=delete $args->{name};
459 120 my $value=delete $args->{value};
460 120 my $checked;
461 120 ▶ if(exists $args->{checked}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'checked'}) { } 32 88 120
462 32 $checked=delete $args->{checked};
463 32 delete $args->{default};
464 }else{
465 88 $checked=delete $args->{default};
466 88 ▶ $value='' unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 40 48 88
467 88 ▶ $checked=grep $_ eq $value,$self->{req}->param($name) if $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"} 44 44 88
468 }
469  
470 120 _escape($name);
471 120 _escape($value);
472  
473 120 my $field=qq(<input type="$type" name="$name" value="$value");
474 120 ▶ $field.=' checked="checked"' if $checked;
Branch: 1 branch
BranchTrueFalseTotal
if $checked 40 80 120
475 120 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 8 120 128
476 8 _escape($key);
477 8 _escape($val);
478 8 $field.=qq( $key="$val");
479 }
480  
481 120 return "$field$self->{well_formed}>";
482 }
483  
484 =item checkbox_group(PAIRLIST)
485  
486 Generates a group of C<checkbox> type C<E<lt>inputE<gt>> elements. If called in
487 list context, returns a list of elements, otherwise a single string containing
488 all tags. All arguments are used directly to generate attributes in each tag,
489 except for those listed below. Arguments with scalar values result in that
490 value being used for each element, whereas hashref values result in the value
491 keyed by the element's C<value> attribute being used.
492 Unless otherwise stated, all names and values are HTML-escaped.
493  
494 C<values>: An arrayref of values.
495 One element will be generated for each element, in the order supplied.
496 If not supplied, the label keys will be used instead.
497  
498 C<labels>: A hashref of labels.
499 Each element generated will be followed by the label keyed
500 by the value. Values will be HTML-escaped unless a false C<escape> argument
501 is supplied. If no label is present for a given value and C<values_as_labels>
502 is true, the value will also be used for the label.
503  
504 C<escape_labels>: If present and false, labels will not be HTML-escaped.
505  
506 C<checked>: Unconditional status. If present, used to decide whether each
507 checkbox is marked as checked, and causes C<default>, C<defaults> and any
508 sticky values to be ignored. May be a single value or arrayref of values.
509  
510 C<default>: Conditional status, ignored if C<checked> is present.
511 If the form is sticky, individual checkboxes are marked as checked if the
512 sticky parameter includes at least one value which is the same as the individual
513 checkbox's value. Otherwise, the supplied C<default> values are
514 used in the same way. May be a single value or arrayref of values.
515  
516 C<linebreak>: If true, each element/label will be followed by a C<E<lt>brE<gt>>
517 element.
518  
519 C<values_as_labels>: If supplied, overrides the form object's
520 C<values_as_labels> attribute.
521  
522 =cut
523  
524 sub checkbox_group{
525 188 ▶ my($self,$args)=&_args;
Subroutine checkbox_group: called CC=22 SCAR=30.9
Pod documented
526 188 ▶ my $type=delete $args->{type} || 'checkbox';
Condition100%2/2
delete $args->{'type'} || 'checkbox'
l!l
9296
MC/DC100%1/1
delete $args->{'type'} || 'checkbox'
delete $args->{'type'}
Truth table100%2/2
A
A= delete $args->{'type'}
Aresult
11
00
527 188 my $name=delete $args->{name};
528 188 ▶ my $labels=delete $args->{labels} || {};
Condition100%2/2
delete $args->{'labels'} || {}
l!l
40148
MC/DC100%1/1
delete $args->{'labels'} || {}
delete $args->{'labels'}
Truth table100%2/2
A
A= delete $args->{'labels'}
Aresult
11
00
529 188 my $escape_labels=1;
530 188 ▶ $escape_labels=delete $args->{escape_labels} if exists $args->{escape_labels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"escape_labels"} 8 180 188
531 188 my $values=delete $args->{values};
532 188 ▶ $values||=[keys %$labels];
Condition100%2/2
$values ||= [keys %$labels]
l!l
14840
MC/DC100%1/1
$values ||= [keys %$labels]
$values
Truth table100%2/2
A
A= $values
Aresult
11
00
533 188 my $checked=[];
534 188 ▶ if(exists $args->{checked}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'checked'}) { } 56 132 188
535 56 $checked=delete $args->{checked};
536 56 ▶ $checked=[$checked] if ref($checked) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $checked ne "ARRAY" 32 24 56
537 56 delete $args->{default};
538 }else{
539 132 ▶ if(exists $args->{default}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"default"}) 16 116 132
540 16 $checked=delete $args->{default};
541 16 ▶ $checked=[$checked] if ref($checked) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $checked ne "ARRAY" 8 8 16
542 }
543 132 ▶ $checked=[$self->{req}->param($name)] if $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"} 66 66 132
544 }
545 188 my %checked=map +($_,'checked'),@$checked;
546 188 ▶ my $br=delete $args->{linebreak} ? "<br$self->{well_formed}>" : '';
Branch: 1 branch
BranchTrueFalseTotal
delete $args->{'linebreak'} ? : 8 180 188
547 188 my $v_as_l=$self->{values_as_labels};
548 188 ▶ if(exists $args->{values_as_labels}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"values_as_labels"}) 16 172 188
549 16 $v_as_l=delete $args->{values_as_labels};
550 }
551  
552 188 _escape($type);
553 188 _escape($name);
554  
555 188 my $field=qq(<input type="$type" name="$name");
556 188 my %per_value=(
557 checked => \%checked,
558 value => {map +($_,$_),@$values},
559 );
560 188 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 8 188 196
561 8 ▶ if($val && ref($val) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if ($val and ref $val eq "HASH") 4 4 8
Condition66%2/3
$val and ref $val eq "HASH"
!ll&&!rl&&r
044
MC/DC50%1/2
$val and ref $val eq "HASH"
$val ref $val eq "HASH"
Truth table66%2/3
A and B
A= $valB= ref $val eq "HASH"
ABresult
0X0
100
111
562 4 $per_value{$key}=$val;
563 4 next;
564 }
565 4 _escape($key);
566 4 _escape($val);
567 4 $field.=qq( $key="$val");
568 }
569  
570 188 my @checkboxes;
571 188 for my $value(@$values){
572 244 my $field=$field;
573 244 ▶ while(my($key,$hash)=each %per_value){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %per_value) 496 244 740
574 496 ▶ exists $hash->{$value}
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{$value} or next 160 336 496
575 or next;
576 336 _escape($key);
577 336 _escape(my $val=$hash->{$value});
578 336 $field.=qq( $key="$val");
579 }
580 244 $field.="$self->{well_formed}>";
581  
582 244 ▶ if(exists $labels->{$value}){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $labels->{$value}) { } 48 196 244
elsif ($v_as_l) { } 24 172 196
583 48 my $label=$labels->{$value};
584 48 ▶ _escape($label) if $escape_labels;
Branch: 1 branch
BranchTrueFalseTotal
if $escape_labels 40 8 48
585 48 $field.=$label;
586 }elsif($v_as_l){
587 24 _escape(my $evalue=$value);
588 24 $field.=$evalue;
589 }
590 244 $field.=$br;
591 244 push @checkboxes,$field;
592 }
593  
594 188 ▶ return @checkboxes if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 188 188
595 188 return join '',@checkboxes;
596 }
597  
598 =item radio(PAIRLIST)
599  
600 As radio_group(), but setting C<type> to C<radio>.
601  
602 =cut
603  
604 sub radio{
605 60 ▶ my($self,$args)=&_args;
Subroutine radio: called CC=2 SCAR=7.1
Pod documented
606 60 ▶ $args->{type}||='radio';
Condition50%1/2
$args->{"type"} ||= "radio"
l!l
060
MC/DC0%0/1
$args->{"type"} ||= "radio"
$args->{"type"}
Truth table50%1/2
A
A= $args->{"type"}
Aresult
11
00
607 60 $self->checkbox($args);
608 }
609  
610 =item radio_group(PAIRLIST)
611  
612 As checkbox_group(), but setting C<type> to C<radio>.
613  
614 =cut
615  
616 sub radio_group{
617 92 ▶ my($self,$args)=&_args;
Subroutine radio_group: called CC=2 SCAR=7.1
Pod documented
618 92 ▶ $args->{type}||='radio';
Condition50%1/2
$args->{"type"} ||= "radio"
l!l
092
MC/DC0%0/1
$args->{"type"} ||= "radio"
$args->{"type"}
Truth table50%1/2
A
A= $args->{"type"}
Aresult
11
00
619 92 $self->checkbox_group($args);
620 }
621  
622 =item select(PAIRLIST)
623  
624 Generates a C<E<lt>selectE<gt>> element. All arguments are used directly to
625 generate attributes in the C<E<lt>selectE<gt>> element, except for those listed below. Unless otherwise stated, all names and values are HTML-escaped.
626  
627 C<values>: An arrayref of values and/or option groups.
628 Scalar values are used directly to create C<E<lt>optionE<gt>> elements,
629 whereas arrayrefs represent option groups. The first element in an option
630 group is either the group's label or a hashref holding all of the group's
631 attributes, of which C<disabled> is special cased to produce the attribute
632 value C<disabled> if true, and no attribute if false.
633 Defaults to label keys.
634  
635 C<labels>: A hashref of labels.
636 Each C<E<lt>optionE<gt>> tag generated will contain the
637 label keyed by its value. If no label is present for a given value, no label
638 will be generated. Defaults to an empty hashref.
639  
640 C<selected>: Unconditional status. If present, the supplied values will be
641 used to decide which options to mark as selected, and C<default> and any
642 sticky values will be ignored. May be a single value or arrayref.
643  
644 C<default>: Conditional status, ignored if C<selected> is
645 supplied. If the form is sticky, the sticky values will be used to decide which
646 options are selected. Otherwise, the supplied values will be used.
647 May be a single value or arrayref.
648  
649 C<multiple>: If true, the C<multiple> attribute is set to C<multiple>.
650  
651 C<values_as_labels>: Overrides the form object's C<values_as_labels> attribute.
652 This is of little value, since it's the default behaviour of HTML in any case.
653  
654 =cut
655  
656 sub select{
657 100 ▶ my($self,$args)=_args(@_);
Subroutine select: called CC=10 SCAR=23.1
Pod documented
658 100 my $name=delete $args->{name};
659 100 my $multiple=delete $args->{multiple};
660 100 ▶ my $labels=delete $args->{labels} || {};
Condition100%2/2
delete $args->{'labels'} || {}
l!l
1684
MC/DC100%1/1
delete $args->{'labels'} || {}
delete $args->{'labels'}
Truth table100%2/2
A
A= delete $args->{'labels'}
Aresult
11
00
661 100 ▶ my $values=delete $args->{values} || [keys %$labels];
Condition100%2/2
delete $args->{'values'} || [keys %$labels]
l!l
8020
MC/DC100%1/1
delete $args->{'values'} || [keys %$labels]
delete $args->{'values'}
Truth table100%2/2
A
A= delete $args->{'values'}
Aresult
11
00
662 100 my $selected;
663 100 ▶ if(exists $args->{selected}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'selected'}) { } 24 76 100
664 24 $selected=delete $args->{selected};
665 24 delete $args->{default};
666 }else{
667 76 $selected=delete $args->{default};
668 76 ▶ $selected=[$self->{req}->param($name)] if $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"} 38 38 76
669 }
670 100 ▶ if(!defined $selected){ $selected=[]; }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $selected) { } 28 72 100
elsif (ref $selected ne 'ARRAY') { } 18 54 72
671 18 elsif(ref($selected) ne 'ARRAY'){ $selected=[$selected]; }
672 100 my %selected=map +($_,1),@$selected;
673 100 my $v_as_l=$self->{values_as_labels};
674 100 ▶ if(exists $args->{values_as_labels}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"values_as_labels"}) 8 92 100
675 8 $v_as_l=delete $args->{values_as_labels};
676 }
677  
678 100 _escape($name);
679 100 my $field=qq(<select name="$name");
680 100 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 0 100 100
681 0 _escape($key);
682 0 _escape($val);
683 0 $field.=qq( $key="$val");
684 }
685 100 ▶ $field.=' multiple="multiple"' if $multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple 4 96 100
686 100 $field.=">";
687  
688 100 $field.=_select_options($values,\%selected,$labels,$v_as_l);
689 100 $field.="</select>";
690  
691 100 $field;
692 }
693  
694  
695  
696 =item submit(PAIRLIST)
697  
698 Generates an C<E<lt>inputE<gt>> of type C<submit>. All of the supplied
699 arguments are HTML-escaped, and used directly as attributes. C<submit>
700 fields are not sticky.
701  
702 =cut
703  
704 sub submit{
705 24 ▶ my($self,$args)=_args(@_);
Subroutine submit: called CC=3 SCAR=11.0
Pod documented
706 24 ▶ $args->{type}='submit' unless exists $args->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"type"} 24 0 24
707  
708 24 my $field='<input';
709 24 ▶ while(my($key,$val)=each %$args){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 52 24 76
710 52 _escape($key);
711 52 _escape($val);
712 52 $field.=qq( $key="$val");
713 }
714 24 $field.="$self->{well_formed}>";
715  
716 24 $field;
717 }
718  
719  
720 =back
721  
722  
723  
724  
725 =begin private
726  
727 =head1 PRIVATE SUBROUTINES
728  
729 These subs are only intended for internal use.
730  
731 =over
732  
733 =item _escape($string)
734  
735 Escape HTML-special characters in $string, in place. If $string is not defined,
736 it will be updated to an empty string.
737  
738 =cut
739  
740 sub _escape($){
741 2067 ▶ if(defined $_[0]){
Subroutine _escape: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1954 113 2067
742 1954 $_[0]=~s/([<>&"]|[^\0-\177])/sprintf "&#%d;",ord $1/ge;
743 }else{
744 113 $_[0]='';
745 }
746 }
747  
748 =item _args(@_)
749  
750 Work out which of the two argument passing conventions is being used, and
751 return ($self,\%args). This essentially converts the public unrolled
752 PAIRLIST arguments into a single hashref, as used by the internal
753 interfaces.
754  
755 =cut
756  
757 sub _args{
758 722 ▶ my $self=shift;
Subroutine _args: called CC=2 SCAR=6.9
759 722 ▶ my $args=ref($_[0]) ? {%{$_[0]}} : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 454 268 722
760 722 ($self,$args);
761 }
762  
763 =item _select_options(\@values,\%selected,\%labels,$values_as_labels)
764  
765 Returns an HTML fragment containing C<option> elements for the supplied
766 list of option values. Values which are arrayrefs are used to represent
767 option groups, wherein the zeroth element is either the group name, or
768 a hashref holding the group's attributes.
769  
770 =cut
771  
772 sub _select_options{
773 116 ▶ my($values,$selected,$labels,$v_as_l)=@_;
Subroutine _select_options: called CC=9 SCAR=22.0
774 116 my $field='';
775 116 for my $value(@$values){
776 164 ▶ if(ref $value){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 16 148 164
777 # Handle option group
778 16 my($_group,@subvalues)=@$value;
779 16 ▶ my %group=ref($_group) ? %$_group : (label => $_group);
Branch: 1 branch
BranchTrueFalseTotal
ref $_group ? : 8 8 16
780 16 ▶ if(delete $group{disabled}){
Branch: 1 branch
BranchTrueFalseTotal
if (delete $group{"disabled"}) 4 12 16
781 4 $group{disabled}='disabled';
782 }
783 16 $field.=qq(<optgroup);
784 16 ▶ while(my($name,$value)=each %group){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %group) 24 16 40
785 24 _escape($value);
786 24 $field.=qq( $name="$value");
787 }
788 16 $field.='>';
789 16 $field.=_select_options(\@subvalues,$selected,$labels);
790 16 $field.='</optgroup>';
791 }else{
792 # Handle single option
793 148 _escape(my $evalue=$value);
794 148 $field.=qq(<option value="$evalue");
795 148 ▶ $field.=' selected="selected"' if $selected->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $selected->{$value} 42 106 148
796 148 $field.=">";
797 148 ▶ if(exists $labels->{$value}){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $labels->{$value}) { } 20 128 148
elsif ($v_as_l) { } 12 116 128
798 20 my $label=$labels->{$value};
799 20 _escape($label);
800 20 $field.=$label;
801 }elsif($v_as_l){
802 12 $field.=$evalue;
803 }
804 148 $field.="</option>";
805 }
806 }
807  
808 116 $field;
809 }
810  
811 =back
812  
813 =end private
814  
815 =cut
816  
817 # Return true to require
818 18 1;
819  
820  
821  
822 =head1 AUTHOR
823  
824 Copyright (C) Institute of Physics Publishing 2000-2011
825  
826 Peter Haworth <pmh@edison.ioppublishing.com>
827  
828 You may use and distribute this module according to the same terms
829 that Perl is distributed under.
830  
831  
« StickyForms.pm ↑ Summary StickyForm/RequestHash.pm »