lib/CGI/FormBuilder/Util.pm

filter: Statementstmt 86.8% 125 / 144 Branchbran 77.5% 62 / 80 Conditioncond 71.7% 61 / 85 MC/DCmcdc 28.5% 12 / 42 Subroutinesub 88.4% 23 / 26 Podpod 100% 20 / 20 totaltotal 76.3% 303 / 397 SCAR 47.0
CC
71
Cov
80%
htmlattr
23.4
arglist
20.0
optsort
19.8
CRAP 109.8
« lib/CGI/FormBuilder/Template/Div.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::Util;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Util - Utility functions for FormBuilder
12  
13 =head1 SYNOPSIS
14  
15 use CGI::FormBuilder::Util;
16  
17 belch "Badness";
18 puke "Egads";
19 debug 2, "Debug message for level 2";
20  
21 =head1 DESCRIPTION
22  
23 This module exports some common utility functions for B<FormBuilder>.
24 These functions are intended for internal use, however I must admit
25 that, from time to time, I just import this module and use some of
26 the routines directly (like C<htmltag()> to generate HTML).
27  
28 =head1 USEFUL FUNCTIONS
29  
30 These can be used directly and are somewhat useful. Don't tell anyone
31 I said that, though.
32  
33 =cut
34  
35 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 10 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # Don't "use" or it collides with our basename()
41 10 require File::Basename;
42  
43 10 our $VERSION = '3.10';
44  
45 # Place functions you want to export by default in the
46 # @EXPORT array. Any other functions can be requested
47 # explicitly if you place them in the @EXPORT_OK array.
48 10 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 10 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 10 our @EXPORT = qw(
51 debug belch puke indent escapeurl escapehtml escapejs
52 autodata optalign optsort optval arglist arghash
53 htmlattr htmltag toname tovar ismember basename rearrange
54 );
55 10 our $DEBUG = 0;
56 10 our %TAGNAMES = (); # holds translated tag names (experimental)
57  
58 # To clean up the HTML, instead of just allowing the HTML tags that
59 # we interpret are "valid", instead we yank out all the options and
60 # stuff that we use internally. This allows arbitrary tags to be
61 # specified in the generation of HTML tags, and also means that this
62 # module doesn't go out of date when the HTML spec changes next week.
63 10 our @OURATTR = qw(
64 add_before_option add_after_option attr autofill autofillshow body bodyname
65 buttonname caller checknum cleanopts columns cookies comment debug delete
66 disable_enter dtd errorname extraname fields fieldattr fieldsubs fieldtype fieldname
67 fieldopts fieldset fieldsets font force formname growable growname header
68 idprefix inputname invalid javascript jsmessage jsname jsprefix jsfunc jshead
69 jserror jsvalid keepextras labels labelname lalign
70 linebreaks message messages nameopts newline NON_EMPTY_SCRIPT other othername
71 optgroups options override page pages pagename params render required
72 reset resetname rowname selectname selectnum sessionidname sessionid
73 smartness source sortopts static statename sticky stylesheet styleclass submit
74 submitname submittedname table tabname template validate values
75 );
76  
77 # trick for speedy lookup
78 10 our %OURATTR = map { $_ => 1 } @OURATTR;
79  
80 # Have to populate ourselves to avoid carp'ing with bad information.
81 # This makes it so deeply-nested calls throw top-level errors, rather
82 # than referring to a sub-module that probably didn't do it.
83 10 our @CARP_NOT = qw(
84 CGI::FormBuilder
85 CGI::FormBuilder::Field
86 CGI::FormBuilder::Field::button
87 CGI::FormBuilder::Field::checkbox
88 CGI::FormBuilder::Field::file
89 CGI::FormBuilder::Field::hidden
90 CGI::FormBuilder::Field::image
91 CGI::FormBuilder::Field::password
92 CGI::FormBuilder::Field::radio
93 CGI::FormBuilder::Field::select
94 CGI::FormBuilder::Field::static
95 CGI::FormBuilder::Field::text
96 CGI::FormBuilder::Field::textarea
97 CGI::FormBuilder::Messages
98 CGI::FormBuilder::Multi
99 CGI::FormBuilder::Source
100 CGI::FormBuilder::Source::File
101 CGI::FormBuilder::Template
102 CGI::FormBuilder::Template::Builtin
103 CGI::FormBuilder::Template::Fast
104 CGI::FormBuilder::Template::HTML
105 CGI::FormBuilder::Template::TT2
106 CGI::FormBuilder::Template::Text
107 CGI::FormBuilder::Template::CGI_SSI
108 CGI::FormBuilder::Util
109 );
110  
111 =head2 debug($level, $string)
112  
113 This prints out the given string only if C<$DEBUG> is greater than
114 the C<$level> specified. For example:
115  
116 $CGI::FormBuilder::Util::DEBUG = 1;
117 debug 1, "this is printed";
118 debug 2, "but not this one";
119  
120 A newline is automatically included, so don't provide one of your own.
121  
122 =cut
123  
124 sub debug ($;@) {
125 38452 ▶ return unless $DEBUG >= $_[0]; # first arg is debug level
Subroutine debug: called CC=2 SCAR=11.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUG >= $_[0] 38452 0 38452
126 0 my $l = shift; # using $_[0] directly above is just a little faster...
127 0 my($func) = (caller(1))[3];
128 #$func =~ s/(.*)::/$1->/;
129 0 warn "[$func] (debug$l) ", @_, "\n";
130 }
131  
132 =head2 belch($string)
133  
134 A modified C<warn> that prints out a better message with a newline added.
135  
136 =cut
137  
138 sub belch (@) {
139 0 ▶ my $i=1;
Subroutine belch: not called CC=1 SCAR=6.9
Pod documented
140 0 carp "[FormBuilder] Warning: ", @_;
141 }
142  
143 =head2 puke($string)
144  
145 A modified C<die> that prints out a useful message.
146  
147 =cut
148  
149 sub puke (@) {
150 8 ▶ my $i=1;
Subroutine puke: called CC=2 SCAR=7.2
Pod documented
151 8 ▶ $DEBUG ? Carp::confess("Fatal: ", @_)
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG ? : 0 8 8
152 : croak "[FormBuilder] Fatal: ", @_
153 }
154  
155 =head2 escapeurl($string)
156  
157 Returns a properly escaped string suitable for including in URL params.
158  
159 =cut
160  
161 sub escapeurl ($) {
162 # minimalist, not 100% correct, URL escaping
163 0 ▶ my $toencode = shift;
Subroutine escapeurl: not called CC=1 SCAR=6.9
Pod documented
164 0 $toencode =~ s!([^a-zA-Z0-9_,.-/])!sprintf("%%%02x",ord($1))!eg;
165 0 return $toencode;
166 }
167  
168 =head2 escapehtml($string)
169  
170 Returns an HTML-escaped string suitable for embedding in HTML tags.
171  
172 =cut
173  
174 sub escapehtml ($) {
175 5644 ▶ my $toencode = shift;
Subroutine escapehtml: called CC=2 SCAR=6.9
Pod documented
176 5644 ▶ return '' unless defined $toencode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $toencode 20 5624 5644
177 # use very basic built-in HTML escaping
178 5624 $toencode =~ s!&!&amp;!g;
179 5624 $toencode =~ s!<!&lt;!g;
180 5624 $toencode =~ s!>!&gt;!g;
181 5624 $toencode =~ s!"!&quot;!g;
182 5624 return $toencode;
183 }
184  
185 =head2 escapejs($string)
186  
187 Returns a string suitable for including in JavaScript. Minimal processing.
188  
189 =cut
190  
191 sub escapejs ($) {
192 400 ▶ my $toencode = shift;
Subroutine escapejs: called CC=1 SCAR=0.0
Pod documented
193 400 $toencode =~ s#'#\\'#g;
194 400 return $toencode;
195 }
196  
197 =head2 htmltag($name, %attr)
198  
199 This generates an XHTML-compliant tag for the name C<$name> based on the
200 C<%attr> specified. For example:
201  
202 my $table = htmltag('table', cellpadding => 1, border => 0);
203  
204 No routines are provided to close tags; you must manually print a closing
205 C<< </table> >> tag.
206  
207 =cut
208  
209 sub htmltag ($;@) {
210 # called as htmltag('tagname', %attr)
211 # creates an HTML tag on the fly, quick and dirty
212 4419 ▶ my $name = shift || return;
Subroutine htmltag: called CC=5 SCAR=16.1
Pod documented
Condition50%1/2
shift() || (return)
l!l
44190
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
213 4419 my $attr = htmlattr($name, @_); # ref return faster
214  
215 # see if we have a special tag name (experimental)
216 4419 (my $look = $name) =~ s#^(/*)##;
217 4419 ▶ $name = "$1$TAGNAMES{$look}" if $TAGNAMES{$look};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Util::TAGNAMES{$look} 82 4337 4419
218  
219 my $htag = join(' ', $name,
220 4419 map { qq($_=") . escapehtml($attr->{$_}) . '"' } sort keys %$attr);
221  
222 4419 ▶ $htag .= ' /' if $name eq 'input' || $name eq 'link'; # XHTML self-closing
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "input" or $name eq "link" 789 3630 4419
Condition100%3/3
$name eq "input" or $name eq "link"
l!l&&r!l&&!r
78543630
MC/DC100%2/2
$name eq "input" or $name eq "link"
$name eq "input" $name eq "link"
Truth table100%3/3
A or B
A= $name eq "input"B= $name eq "link"
ABresult
1X1
011
000
223 4419 return '<' . $htag . '>';
224 }
225  
226 =head2 htmlattr($name, %attr)
227  
228 This cleans any internal B<FormBuilder> attributes from the specified tag.
229 It is automatically called by C<htmltag()>.
230  
231 =cut
232  
233 sub htmlattr ($;@) {
234 # called as htmlattr('tagname', %attr)
235 # returns valid HTML attr for that tag
236 4548 ▶ my $name = shift || return;
Subroutine htmlattr: called CC=23 SCAR=31.5
Pod documented
Condition50%1/2
shift() || (return)
l!l
45480
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
237 4548 ▶ my $attr = ref $_[0] ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1481 3067 4548
238 4548 my %html;
239 4548 ▶ while (my($key,$val) = each %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 16605 4548 21153
240 # Anything but normal scalar data gets yanked
241 16605 ▶ next if ref $val || ! defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val or not defined $val 4071 12534 16605
Condition100%3/3
ref $val or not defined $val
l!l&&r!l&&!r
2276179512534
MC/DC100%2/2
ref $val or not defined $val
ref $val not defined $val
Truth table100%3/3
A or not(B)
A= ref $valB= not defined $val
ABresult
1X1
011
000
242  
243 # This cleans out all the internal junk kept in each data
244 # element, returning everything else (for an html tag).
245 # Crap, I used "text" here and body takes a text attr!!
246 12534 ▶ next if ($OURATTR{$key} || $key =~ /^_/
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select" or $key eq "type" and $name eq "select" or $key eq "label" and ($name ne "optgroup" and $name ne "option") or $key eq "title" and $name eq "form" 200 4931 5131
Condition86%31/36
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select" or $key eq "type" and $name eq "select" or $key eq "label" and ($name ne "optgroup" and $name ne "option") or $key eq "title" and $name eq "form"
l!l&&r!l&&!r
74824562490
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select" or $key eq "type" and $name eq "select" or $key eq "label" and ($name ne "optgroup" and $name ne "option")
l!l&&r!l&&!r
7403795052
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select" or $key eq "type" and $name eq "select"
l!l&&r!l&&!r
740305131
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select"
l!l&&r!l&&!r
7357465131
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body"
l!l&&r!l&&!r
72281295177
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/
l!l&&r!l&&!r
70991295306
$key eq "text" and $name ne "body"
!ll&&!rl&&r
51770129
$key eq "multiple" and $name ne "select"
!ll&&!rl&&r
51151646
$key eq "type" and $name eq "select"
!ll&&!rl&&r
43327990
$key eq "label" and ($name ne "optgroup" and $name ne "option")
!ll&&!rl&&r
50421079
$name ne "optgroup" and $name ne "option"
!ll&&!rl&&r
10079
$key eq "title" and $name eq "form"
!ll&&!rl&&r
49310121
MC/DC0%0/13
$CGI::FormBuilder::Util::OURATTR{$key} or $key =~ /^_/ or $key eq "text" and $name ne "body" or $key eq "multiple" and $name ne "select" or $key eq "type" and $name eq "select" or $key eq "label" and ($name ne "optgroup" and $name ne "option") or $key eq "title" and $name eq "form"
$CGI::FormBuilder::Util::OURATTR{$key} $key =~ /^_/ $key eq "text" $name ne "body" $key eq "multiple" $name ne "select" $key eq "type" $name eq "select" $key eq "label" $name ne "optgroup" $name ne "option" $key eq "title" $name eq "form"
Truth table6%6/89
A or B or C and D or E and F or G and H or I and J and K or L and M
A= $CGI::FormBuilder::Util::OURATTR{$key}B= $key =~ /^_/C= $key eq "text"D= $name ne "body"E= $key eq "multiple"F= $name ne "select"G= $key eq "type"H= $name eq "select"I= $key eq "label"J= $name ne "optgroup"K= $name ne "option"L= $key eq "title"M= $name eq "form"
ABCDEFGHIJKLMresult
1XXXXXXXXXXXX1
01XXXXXXXXXXX1
0011XXXXXXXXX1
000X11XXXXXXX1
001011XXXXXXX1
000X0X11XXXXX1
000X1011XXXXX1
00100X11XXXXX1
00101011XXXXX1
000X0X0X111XX1
000X0X10111XX1
000X100X111XX1
000X1010111XX1
00100X0X111XX1
00100X10111XX1
0010100X111XX1
00101010111XX1
000X0X0X0XX111
000X0X0X10X111
000X0X0X110111
000X0X100XX111
000X0X1010X111
000X0X10110111
000X100X0XX111
000X100X10X111
000X100X110111
000X10100XX111
000X101010X111
000X1010110111
00100X0X0XX111
00100X0X10X111
00100X0X110111
00100X100XX111
00100X1010X111
00100X10110111
0010100X0XX111
0010100X10X111
0010100X110111
001010100XX111
0010101010X111
00101010110111
000X0X0X0XX0X0
000X0X0X0XX100
000X0X0X10X0X0
000X0X0X10X100
000X0X0X1100X0
000X0X0X110100
000X0X100XX0X0
000X0X100XX100
000X0X1010X0X0
000X0X1010X100
000X0X101100X0
000X0X10110100
000X100X0XX0X0
000X100X0XX100
000X100X10X0X0
000X100X10X100
000X100X1100X0
000X100X110100
000X10100XX0X0
000X10100XX100
000X101010X0X0
000X101010X100
000X10101100X0
000X1010110100
00100X0X0XX0X0
00100X0X0XX100
00100X0X10X0X0
00100X0X10X100
00100X0X1100X0
00100X0X110100
00100X100XX0X0
00100X100XX100
00100X1010X0X0
00100X1010X100
00100X101100X0
00100X10110100
0010100X0XX0X0
0010100X0XX100
0010100X10X0X0
0010100X10X100
0010100X1100X0
0010100X110100
001010100XX0X0
001010100XX100
0010101010X0X0
0010101010X100
001010101100X0
00101010110100
247 || ($key eq 'text' && $name ne 'body')
248 || ($key eq 'multiple' && $name ne 'select')
249 || ($key eq 'type' && $name eq 'select')
250 || ($key eq 'label' && ($name ne 'optgroup' && $name ne 'option'))
251 || ($key eq 'title' && $name eq 'form'));
252  
253 # see if we have a special tag name (experimental)
254 4931 ▶ $key = $TAGNAMES{$key} if $TAGNAMES{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::FormBuilder::Util::TAGNAMES{$key} 270 4661 4931
255 4931 $html{$key} = $val;
256 }
257 # "double-name" fields with an id for easier DOM scripting
258 # do not override explictly set id attributes
259 4548 ▶ $html{id} = tovar($html{name}) if exists $html{name} and not exists $html{id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $html{"name"} and not exists $html{"id"} 365 4183 4548
Condition100%3/3
exists $html{"name"} and not exists $html{"id"}
!ll&&!rl&&r
3924259365
MC/DC100%2/2
exists $html{"name"} and not exists $html{"id"}
exists $html{"name"} not exists $html{"id"}
Truth table100%3/3
A and not(B)
A= exists $html{"name"}B= not exists $html{"id"}
ABresult
0X0
100
111
260  
261 4548 ▶ return wantarray ? %html : \%html;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 129 4419 4548
262 }
263  
264 =head2 toname($string)
265  
266 This is responsible for the auto-naming functionality of B<FormBuilder>.
267 Since you know Perl, it's easiest to just show what it does:
268  
269 $name =~ s!\.\w+$!!; # lose trailing ".suf"
270 $name =~ s![^a-zA-Z0-9.-/]+! !g; # strip non-alpha chars
271 $name =~ s!\b(\w)!\u$1!g; # convert _ to space/upper
272  
273 This results in something like "cgi_script.pl" becoming "Cgi Script".
274  
275 =cut
276  
277 sub toname ($) {
278 # creates a name from a var/file name (like file2name)
279 714 ▶ my $name = shift;
Subroutine toname: called CC=1 SCAR=0.0
Pod documented
280 714 $name =~ s!\.\w+$!!; # lose trailing ".suf"
281 714 $name =~ s![^a-zA-Z0-9.-/]+! !g; # strip non-alpha chars
282 714 $name =~ s!\b(\w)!\u$1!g; # convert _ to space/upper
283 714 return $name;
284 }
285  
286 =head2 tovar($string)
287  
288 Turns a string into a variable name. Basically just strips C<\W>,
289 and prefixes "fb_" on the front of it.
290  
291 =cut
292  
293 sub tovar ($) {
294 995 ▶ my $name = shift;
Subroutine tovar: called CC=1 SCAR=0.0
Pod documented
295 995 $name =~ s#\W+#_#g;
296 995 $name =~ tr/_//s; # squish __ accidentally
297 995 $name =~ s/_$//; # trailing _ on "[Yo!]"
298 995 return $name;
299 }
300  
301 =head2 ismember($el, @array)
302  
303 Returns true if C<$el> is in C<@array>
304  
305 =cut
306  
307 sub ismember ($@) {
308 # returns 1 if is in set, undef otherwise
309 # do so case-insensitively
310 777 ▶ my $test = lc shift;
Subroutine ismember: called CC=3 SCAR=11.0
Pod documented
311 777 for (@_) {
312 860 ▶ return 1 if $test eq lc $_;
Branch: 1 branch
BranchTrueFalseTotal
if $test eq lc $_ 160 700 860
313 }
314 617 return;
315 }
316  
317 =head1 USELESS FUNCTIONS
318  
319 These are totally useless outside of B<FormBuilder> internals.
320  
321 =head2 autodata($ref)
322  
323 This dereferences C<$ref> and returns the underlying data. For example:
324  
325 %hash = autodata($hashref);
326 @array = autodata($arrayref);
327  
328 =cut
329  
330 sub autodata ($) {
331 # auto-derefs appropriately
332 2945 ▶ my $data = shift;
Subroutine autodata: called CC=7 SCAR=19.9
Pod documented
333 2945 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 767 2178 2945
334 2178 ▶ if (my $ref = ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $data) 1007 1171 2178
335 1007 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 960 47 1007
elsif ($ref eq 'HASH') { } 47 0 47
336 960 ▶ return wantarray ? @{$data} : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 960 0 960
337 } elsif ($ref eq 'HASH') {
338 47 ▶ return wantarray ? %{$data} : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 47 0 47
339 } else {
340 0 puke "Sorry, can't handle odd data ref '$ref' (only ARRAY or HASH)";
341 }
342 }
343 1171 return $data; # return as-is
344 }
345  
346 =head2 arghash(@_)
347  
348 This returns a hash of options passed into a sub:
349  
350 sub field {
351 my $self = shift;
352 my %opt = arghash(@_);
353 }
354  
355 It will return a hashref in scalar context.
356  
357 =cut
358  
359 sub arghash (;@) {
360 2124 ▶ return $_[0] if ref $_[0] && ! wantarray;
Subroutine arghash: called CC=6 SCAR=18.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and not wantarray 392 1732 2124
Condition66%2/3
ref $_[0] and not wantarray
!ll&&!rl&&r
17320392
MC/DC50%1/2
ref $_[0] and not wantarray
ref $_[0] not wantarray
Truth table66%2/3
A and not(B)
A= ref $_[0]B= not wantarray
ABresult
0X0
100
111
361  
362 1732 ▶ belch "Odd number of arguments passed into ", (caller(1))[3]
Branch: 1 branch
BranchTrueFalseTotal
if @_ and @_ % 2 != 0 0 1732 1732
Condition66%2/3
@_ and @_ % 2 != 0
!ll&&!rl&&r
9967360
MC/DC0%0/2
@_ and @_ % 2 != 0
@_ @_ % 2 != 0
Truth table66%2/3
A and B
A= @_B= @_ % 2 != 0
ABresult
0X0
100
111
363 if @_ && @_ % 2 != 0;
364  
365 1732 ▶ return wantarray ? @_ : { @_ }; # assume scalar hashref
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 144 1588 1732
366 }
367  
368 =head2 arglist(@_)
369  
370 This returns a list of args passed into a sub:
371  
372 sub value {
373 my $self = shift;
374 $self->{value} = arglist(@_);
375  
376 It will return an arrayref in scalar context.
377  
378 =cut
379  
380 sub arglist (;@) {
381 0 ▶ return $_[0] if ref $_[0] && ! wantarray;
Subroutine arglist: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and not wantarray 0 0 0
Condition0%0/3
ref $_[0] and not wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and not wantarray
ref $_[0] not wantarray
Truth table0%0/3
A and not(B)
A= ref $_[0]B= not wantarray
ABresult
0X0
100
111
382 0 ▶ return wantarray ? @_ : [ @_ ]; # assume scalar arrayref
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
383 }
384  
385 =head2 indent($num)
386  
387 A simple sub that returns 4 spaces x C<$num>. Used to indent code.
388  
389 =cut
390  
391 sub indent (;$) {
392 # return proper spaces to indent x 4 (code prettification)
393 426 ▶ return ' ' x shift();
Subroutine indent: called CC=1 SCAR=0.0
Pod documented
394 }
395  
396 =head2 optalign(\@opt)
397  
398 This returns the options specified as an array of arrayrefs, which
399 is what B<FormBuilder> expects internally.
400  
401 =cut
402  
403 sub optalign ($) {
404 # This creates and returns the options needed based
405 # on an $opt array/hash shifted in
406 496 ▶ my $opt = shift;
Subroutine optalign: called CC=6 SCAR=18.6
Pod documented
407  
408 # "options" are the options for our select list
409 496 my @opt = ();
410 496 ▶ if (my $ref = ref $opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $opt) { } 246 250 496
411 246 ▶ if ($ref eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "CODE") 4 242 246
412 # exec to get options
413 4 $opt = &$opt;
414 }
415 # we turn any data into ( ['key', 'val'], ['key', 'val'] )
416 # have to check sub-data too, hence why this gets a little nasty
417 @opt = ($ref eq 'HASH')
418 ? map { (ref $opt->{$_} eq 'ARRAY')
419 0 ▶ ? [$_, $opt->{$_}[0]] : [$_, $opt->{$_}] } keys %{$opt}
Branch: 1 branch
BranchTrueFalseTotal
ref $opt->{$_} eq 'ARRAY' ? : 0 0 0
420 246 ▶ : map { (ref $_ eq 'HASH') ? [ %{$_} ] : $_ } autodata $opt;
Branch: 2 branches
BranchTrueFalseTotal
$ref eq 'HASH' ? : 0 246 246
ref $_ eq 'HASH' ? : 20 1211 1231
421 } else {
422 # this code should not be reached, but is here for safety
423 250 @opt = ($opt);
424 }
425  
426 496 return @opt;
427 }
428  
429 =head2 optsort($sortref, @opt)
430  
431 This sorts and returns the options based on C<$sortref>. It expects
432 C<@opt> to be in the format returned by C<optalign()>. The C<$sortref>
433 spec can be the string C<NAME>, C<NUM>, or a reference to a C<&sub>
434 which takes pairs of values to compare.
435  
436 =cut
437  
438 sub optsort ($@) {
439 # pass in the sort and ref to opts
440 18 ▶ my $sort = shift;
Subroutine optsort: called CC=12 SCAR=29.8
Pod documented
441 18 my @opt = @_;
442  
443 18 debug 2, "optsort($sort) called for field";
444  
445 # Currently any CODEREF can only sort on the value, which sucks if the
446 # value and label are substantially different. This is caused by the fact
447 # that options as specified by the user only have one element, not two
448 # as hashes or generated options do. This should really be an option,
449 # since sometimes you want the labels sorted too. Patches welcome.
450 18 ▶ if ($sort eq 'alpha' || $sort eq 'name' || $sort eq 'NAME' || $sort eq 1) {
Branch: 5 branches
BranchTrueFalseTotal
if ($sort eq 'alpha' or $sort eq 'name' or $sort eq 'NAME' or $sort eq 1) { } 6 12 18
elsif ($sort eq 'numeric' or $sort eq 'num' or $sort eq 'NUM') { } 6 6 12
elsif ($sort eq 'LABELNAME' or $sort eq 'LABEL') { } 0 6 6
elsif ($sort eq 'LABELNUM') { } 0 6 6
elsif (ref $sort eq 'CODE') { } 6 0 6
Condition55%5/9
$sort eq "alpha" or $sort eq "name" or $sort eq "NAME" or $sort eq 1
l!l&&r!l&&!r
6012
$sort eq "alpha" or $sort eq "name" or $sort eq "NAME"
l!l&&r!l&&!r
0612
$sort eq "alpha" or $sort eq "name"
l!l&&r!l&&!r
0018
MC/DC25%1/4
$sort eq "alpha" or $sort eq "name" or $sort eq "NAME" or $sort eq 1
$sort eq "alpha" $sort eq "name" $sort eq "NAME" $sort eq 1
Truth table40%2/5
A or B or C or D
A= $sort eq "alpha"B= $sort eq "name"C= $sort eq "NAME"D= $sort eq 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
451 6 @opt = sort { (autodata($a))[0] cmp (autodata($b))[0] } @opt;
452 ▶ } elsif ($sort eq 'numeric' || $sort eq 'num' || $sort eq 'NUM') {
Condition50%3/6
$sort eq "numeric" or $sort eq "num" or $sort eq "NUM"
l!l&&r!l&&!r
066
$sort eq "numeric" or $sort eq "num"
l!l&&r!l&&!r
0012
MC/DC33%1/3
$sort eq "numeric" or $sort eq "num" or $sort eq "NUM"
$sort eq "numeric" $sort eq "num" $sort eq "NUM"
Truth table50%2/4
A or B or C
A= $sort eq "numeric"B= $sort eq "num"C= $sort eq "NUM"
ABCresult
1XX1
01X1
0011
0000
453 6 @opt = sort { (autodata($a))[0] <=> (autodata($b))[0] } @opt;
454 ▶ } elsif ($sort eq 'LABELNAME' || $sort eq 'LABEL') {
Condition33%1/3
$sort eq "LABELNAME" or $sort eq "LABEL"
l!l&&r!l&&!r
006
MC/DC0%0/2
$sort eq "LABELNAME" or $sort eq "LABEL"
$sort eq "LABELNAME" $sort eq "LABEL"
Truth table33%1/3
A or B
A= $sort eq "LABELNAME"B= $sort eq "LABEL"
ABresult
1X1
011
000
455 0 @opt = sort { (autodata($a))[1] cmp (autodata($b))[1] } @opt;
456 } elsif ($sort eq 'LABELNUM') {
457 0 @opt = sort { (autodata($a))[1] <=> (autodata($b))[1] } @opt;
458 } elsif (ref $sort eq 'CODE') {
459 6 @opt = sort { eval &{$sort}((autodata($a))[0], (autodata($b))[0]) } @opt;
460 } else {
461 0 puke "Unsupported sort type '$sort' specified - must be 'NAME' or 'NUM'";
462 }
463  
464 # return our options
465 18 return @opt;
466 }
467  
468 =head2 optval($opt)
469  
470 This takes one of the elements of C<@opt> and returns it split up.
471 Useless outside of B<FormBuilder>.
472  
473 =cut
474  
475 sub optval ($) {
476 913 ▶ my $opt = shift;
Subroutine optval: called CC=3 SCAR=11.0
Pod documented
477 913 ▶ my @ary = (ref $opt eq 'ARRAY') ? @{$opt} : ($opt);
Branch: 1 branch
BranchTrueFalseTotal
ref $opt eq 'ARRAY' ? : 206 707 913
478 913 ▶ return wantarray ? @ary : $ary[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 913 0 913
479 }
480  
481 =head2 rearrange($ref, $name)
482  
483 Rearranges arguments designed to be per-field from the global inheritor.
484  
485 =cut
486  
487 sub rearrange {
488 1727 ▶ my $from = shift;
Subroutine rearrange: called CC=6 SCAR=17.9
Pod documented
489 1727 my $name = shift;
490 1727 my $ref = ref $from;
491 1727 my $tval;
492 1727 ▶ if ($ref && $ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'HASH') { } 166 1561 1727
elsif ($ref and $ref eq 'ARRAY') { } 126 1435 1561
Condition100%3/3
$ref and $ref eq "HASH"
!ll&&!rl&&r
1435126166
MC/DC100%2/2
$ref and $ref eq "HASH"
$ref $ref eq "HASH"
Truth table100%3/3
A and B
A= $refB= $ref eq "HASH"
ABresult
0X0
100
111
493 166 $tval = $from->{$name};
494 ▶ } elsif ($ref && $ref eq 'ARRAY') {
Condition66%2/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
14350126
MC/DC50%1/2
$ref and $ref eq "ARRAY"
$ref $ref eq "ARRAY"
Truth table66%2/3
A and B
A= $refB= $ref eq "ARRAY"
ABresult
0X0
100
111
495 126 ▶ $tval = ismember($name, @$from) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&ismember($name, @$from) ? : 69 57 126
496 } else {
497 1435 $tval = $from;
498 }
499 1727 return $tval;
500 }
501  
502 =head2 basename
503  
504 Returns the script name or $0 hacked up to the first dir
505  
506 =cut
507  
508 sub basename () {
509 # Windows sucks so bad it's amazing to me.
510 14 ▶ my $prog = File::Basename::basename($ENV{SCRIPT_NAME} || $0);
Subroutine basename: called CC=3 SCAR=12.0
Pod documented
Condition33%1/3
$ENV{'SCRIPT_NAME'} || $0
l!l&&r!l&&!r
0140
MC/DC0%0/2
$ENV{'SCRIPT_NAME'} || $0
$ENV{'SCRIPT_NAME'} $0
Truth table33%1/3
A || B
A= $ENV{'SCRIPT_NAME'}B= $0
ABresult
1X1
011
000
511 14 $prog =~ s/\?.*//; # lose ?p=v
512 14 ▶ belch "Script basename() undefined somehow" unless $prog;
Branch: 1 branch
BranchTrueFalseTotal
unless $prog 0 14 14
513 14 return $prog;
514 }
515  
516 10 1;
517 __END__
« lib/CGI/FormBuilder/Template/Div.pm ↑ Summary lib/CGI/FormBuilder/Multi.pm »