File Coverage

blib/lib/Format/Util/Strings.pm
Criterion Covered Total %
statement 48 48 100.0
branch 15 20 75.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 76 81 93.8


line stmt bran cond sub pod time code
1             package Format::Util::Strings;
2              
3 1     1   126793 use 5.006;
  1         8  
4 1     1   5 use strict;
  1         3  
  1         42  
5 1     1   6 use warnings FATAL => 'all';
  1         2  
  1         43  
6              
7 1     1   5 use Carp qw(croak);
  1         2  
  1         42  
8 1     1   571 use Encode;
  1         10340  
  1         72  
9              
10 1     1   8 use base 'Exporter';
  1         2  
  1         326  
11             our @EXPORT_OK = qw( defang defang_lite set_selected_item );
12              
13             =head1 NAME
14              
15             Format::Util::Strings - Miscellaneous routines to do with manipulating strings
16              
17             =cut
18              
19             our $VERSION = '0.17'; ## VERSION
20              
21             =head1 SYNOPSIS
22              
23             Quick summary of what the module does.
24              
25             Perhaps a little code snippet.
26              
27             use Format::Util::Strings;
28              
29             my $foo = Format::Util::Strings->new();
30             ...
31              
32             =head1 EXPORT
33              
34             defang
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 defang
39              
40             Removes potentially dangerous characters from input strings.
41             You should probably be using Untaint.
42              
43             =cut
44              
45             sub defang {
46 4     4 1 9 my ($string) = @_;
47              
48 4 100       17 return '' if not defined $string;
49 3 50       17 return '' if length $string < 0;
50              
51 3         7 $string = defang_lite($string);
52 3         6 $string =~ tr[/~%][ ];
53 3         13 return $string;
54             }
55              
56             =head2 defang_lite
57              
58             Removes potentially dangerous characters from input strings.
59             You should probably be using Untaint.
60              
61             defang_lite is a lighter version that is not so restrictive as defang
62              
63             =cut
64              
65             sub defang_lite {
66 8     8 1 1755 my ($string) = @_;
67              
68 8 100       26 return '' if not defined $string;
69 7 50       17 return '' if length $string < 0;
70              
71 7 50       33 $string = Encode::decode('UTF-8', $string) unless Encode::is_utf8($string);
72              
73 7 100       536 if (length($string) > 500) { $string = substr($string, 0, 500); }
  2         6  
74              
75 7         32 $string =~ tr[\0"{}\\^`><\n\r\f\t][ ];
76 1     1   601 $string =~ s/[\P{IsPrint}]/ /g;
  1         15  
  1         15  
  7         30  
77              
78 7         26 return $string;
79             }
80              
81             =head2 set_selected_item($selecteditem, $optionlist)
82              
83             Sets the selected item in an
84             Params :
85             - $selecteditem : the value of the item (usually taken from %input)
86             - $optionlist : The option list, as either an HTML string or a hash ref conforming to our oop::Form standard.
87             Returns : If hash ref given, 1 if selected item is set, false otherwise
88             If HTML given, the altered HTML
89              
90             =cut
91              
92             sub set_selected_item {
93 2     2 1 2891 my ($selecteditem, $optionlist) = @_;
94              
95 2         3 my $ret_val;
96 2 100       12 if (ref $optionlist eq 'HASH') {
    50          
97             OPTION:
98 1         2 foreach my $option (@{$optionlist->{'input'}->{'options'}}) {
  1         4  
99 2 100       8 if ($option->{'value'} eq $selecteditem) {
100 1         3 $option->{'selected'} = 'selected';
101 1         2 $ret_val = 1;
102             }
103             }
104             } elsif ($optionlist) {
105 1         2 $ret_val = $optionlist;
106 1 50       3 if ($selecteditem) {
107 1         36 $ret_val =~ s/(value\=[\"\']?$selecteditem[\"\']?\>)/selected="selected" $1/i;
108 1         14 $ret_val =~ s/(\$selecteditem)/
109             }
110             }
111 2         10 return $ret_val;
112             }
113              
114             =head1 AUTHOR
115              
116             binary.com, C<< >>
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests to C, or through
121             the web interface at L. I will be notified, and then you'll
122             automatically be notified of progress on your bug as I make changes.
123              
124              
125              
126              
127             =head1 SUPPORT
128              
129             You can find documentation for this module with the perldoc command.
130              
131             perldoc Format::Util::Strings
132              
133              
134             You can also look for information at:
135              
136             =over 4
137              
138             =item * RT: CPAN's request tracker (report bugs here)
139              
140             L
141              
142             =item * AnnoCPAN: Annotated CPAN documentation
143              
144             L
145              
146             =item * CPAN Ratings
147              
148             L
149              
150             =item * Search CPAN
151              
152             L
153              
154             =back
155              
156              
157             =head1 ACKNOWLEDGEMENTS
158              
159              
160             =head1 LICENSE AND COPYRIGHT
161              
162             Copyright 2014 binary.com.
163              
164             This program is free software; you can redistribute it and/or modify it
165             under the terms of the the Artistic License (2.0). You may obtain a
166             copy of the full license at:
167              
168             L
169              
170             Any use, modification, and distribution of the Standard or Modified
171             Versions is governed by this Artistic License. By using, modifying or
172             distributing the Package, you accept this license. Do not use, modify,
173             or distribute the Package, if you do not accept this license.
174              
175             If your Modified Version has been derived from a Modified Version made
176             by someone other than you, you are nevertheless required to ensure that
177             your Modified Version complies with the requirements of this license.
178              
179             This license does not grant you the right to use any trademark, service
180             mark, tradename, or logo of the Copyright Holder.
181              
182             This license includes the non-exclusive, worldwide, free-of-charge
183             patent license to make, have made, use, offer to sell, sell, import and
184             otherwise transfer the Package with respect to any patent claims
185             licensable by the Copyright Holder that are necessarily infringed by the
186             Package. If you institute patent litigation (including a cross-claim or
187             counterclaim) against any party alleging that the Package constitutes
188             direct or contributory patent infringement, then this Artistic License
189             to you shall terminate on the date that such litigation is filed.
190              
191             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
192             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
193             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
194             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
195             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
196             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
197             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
198             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199              
200              
201             =cut
202              
203             1; # End of Format::Util::Strings