| 1 | package Format::Util::Strings; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use Encode; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 2 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | 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 | 2 | our $VERSION = '0.18'; ## 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 | 4 | ▶ | sub defang { | ||||||||||||
|
Subroutine
defang: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 46 | 4 | my ($string) = @_; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 4 | ▶ | return '' if not defined $string; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 3 | ▶ | return '' if length $string < 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 3 | $string = defang_lite($string); | |||||||||||||
| 52 | 3 | $string =~ tr[/~%][ ]; | |||||||||||||
| 53 | 3 | 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 | 8 | ▶ | sub defang_lite { | ||||||||||||
|
Subroutine
defang_lite: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 66 | 8 | my ($string) = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 8 | ▶ | return '' if not defined $string; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 7 | ▶ | return '' if length $string < 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | 7 | ▶ | $string = Encode::decode('UTF-8', $string) unless Encode::is_utf8($string); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | |||||||||||||||
| 73 | 7 | ▶ | if (length($string) > 500) { $string = substr($string, 0, 500); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | |||||||||||||||
| 75 | 7 | $string =~ tr[\0"{}\\^`><\n\r\f\t][ ]; | |||||||||||||
| 76 | 7 | $string =~ s/[\P{IsPrint}]/ /g; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 7 | return $string; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head2 set_selected_item($selecteditem, $optionlist) | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Sets the selected item in an <option> list. | ||||||||||||||
| 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 | 2 | ▶ | sub set_selected_item { | ||||||||||||
|
Subroutine
set_selected_item: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||
| 93 | 2 | my ($selecteditem, $optionlist) = @_; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 2 | my $ret_val; | |||||||||||||
| 96 | 2 | ▶ | if (ref $optionlist eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 97 | OPTION: | ||||||||||||||
| 98 | 1 | foreach my $option (@{$optionlist->{'input'}->{'options'}}) { | |||||||||||||
| 99 | 2 | ▶ | if ($option->{'value'} eq $selecteditem) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 1 | $option->{'selected'} = 'selected'; | |||||||||||||
| 101 | 1 | $ret_val = 1; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | } | ||||||||||||||
| 104 | } elsif ($optionlist) { | ||||||||||||||
| 105 | 1 | $ret_val = $optionlist; | |||||||||||||
| 106 | 1 | ▶ | if ($selecteditem) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 1 | $ret_val =~ s/(value\=[\"\']?$selecteditem[\"\']?\>)/selected="selected" $1/i; | |||||||||||||
| 108 | 1 | $ret_val =~ s/(\<OPTION\>$selecteditem)/<option selected="selected">$selecteditem/i; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | } | ||||||||||||||
| 111 | 2 | return $ret_val; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||
| 115 | |||||||||||||||
| 116 | binary.com, C<< <rakesh at binary.com> >> | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 BUGS | ||||||||||||||
| 119 | |||||||||||||||
| 120 | Please report any bugs or feature requests to C<bug-format-util at rt.cpan.org>, or through | ||||||||||||||
| 121 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Format-Util>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Format-Util> | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 143 | |||||||||||||||
| 144 | L<http://annocpan.org/dist/Format-Util> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =item * CPAN Ratings | ||||||||||||||
| 147 | |||||||||||||||
| 148 | L<http://cpanratings.perl.org/d/Format-Util> | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =item * Search CPAN | ||||||||||||||
| 151 | |||||||||||||||
| 152 | L<http://search.cpan.org/dist/Format-Util/> | ||||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||
| 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 | 2 | 1; # End of Format::Util::Strings | |||||||||||||