blib/lib/CGI/Untaint/CountyStateProvince/US.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 90.1% 64 / 71 CC 10 SCAR 17.5
CC
10
Cov
91%
is_valid
9.2
value
2.0
BEGIN
1.0
CRAP 5.7
↑ Summary
1 package CGI::Untaint::CountyStateProvince::US;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Locale::SubCountry;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # use base qw(CGI::Untaint::object CGI::Untaint::CountyStateProvince);
8 4 ▶use base 'CGI::Untaint::object';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 CGI::Untaint::CountyStateProvince::US - Add U.S. states to CGI::Untaint::CountyStateProvince tables
13  
14 =head1 VERSION
15  
16 Version 0.05
17  
18 =cut
19  
20 4 our $VERSION = '0.05';
21  
22 =head1 SYNOPSIS
23  
24 The module validates that a given input,
25 represents a valid U.S. state.
26 It supports both full state names (e.g., "Maryland") and two-letter state abbreviations (e.g., "MD").
27  
28 Adds a list of U.S. states to the list of counties/states/provinces
29 which are known by the CGI::Untaint::CountyStateProvince validator allowing you
30 to verify that a field in an HTML form contains a valid U.S. state.
31  
32 You must include CGI::Untaint::CountyStateProvince::US after including
33 CGI::Untaint, otherwise it won't work.
34  
35 use CGI::Info;
36 use CGI::Untaint;
37 use CGI::Untaint::CountyStateProvince::US;
38 my $info = CGI::Info->new();
39 my $u = CGI::Untaint->new($info->params());
40 # Succeeds if state = 'MD' or 'Maryland', fails if state = 'Queensland';
41 $u->extract(-as_CountyStateProvince => 'state');
42 # ...
43  
44 =cut
45  
46 =head1 SUBROUTINES/METHODS
47  
48 =head2 is_valid
49  
50 Validates the data, setting the data to be the two-letter abbreviation for the
51 given state. See CGI::Untaint::is_valid.
52  
53 =cut
54  
55 12 ▶sub is_valid {
Subroutine is_valid: called CC=9 SCAR=22.2
Pod documented
56 12 my $self = shift;
57  
58 12 my $value = uc($self->value());
59 12 $value =~ s/^\s+|\s+$//g; # Trim whitespace
60  
61 12 ▶ if($value =~ /([A-Z][A-Z\s]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /([A-Z][A-Z\s]+)/) { } 11 1 12
62 11 $value = $1;
63 } else {
64 1 return 0;
65 }
66  
67 11 ▶ unless($self->{_validator}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_validator"}) 2 9 11
68 2 $self->{_validator} = Locale::SubCountry->new('US');
69 2 ▶ unless($self->{_validator}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_validator"}) 0 2 2
70 0 return 0;
71 }
72 }
73  
74 11 my $state = $self->{_validator}->code($value);
75 11 ▶ if($state && ($state ne 'unknown')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state and $state ne "unknown") 3 8 11
Condition66%2/3
$state and $state ne "unknown"
!ll&&!rl&&r
083
MC/DC50%1/2
$state and $state ne "unknown"
$state $state ne "unknown"
Truth table66%2/3
A and B
A= $stateB= $state ne "unknown"
ABresult
0X0
100
111
76 # Given full state name
77 # Detaintify
78 3 ▶ if($state =~ /(^[A-Z]{2}$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /(^[A-Z]{2}$)/) 3 0 3
79 3 return $1;
80 }
81 }
82  
83 8 $state = $self->{_validator}->full_name($value);
84 8 ▶ if($state && ($state ne 'unknown')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state and $state ne "unknown") 4 4 8
Condition66%2/3
$state and $state ne "unknown"
!ll&&!rl&&r
044
MC/DC50%1/2
$state and $state ne "unknown"
$state $state ne "unknown"
Truth table66%2/3
A and B
A= $stateB= $state ne "unknown"
ABresult
0X0
100
111
85 # Given two-letter abbreviation
86 4 return $value;
87 }
88  
89 4 return 0;
90 }
91  
92 =head2 value
93  
94 Sets the raw data to be validated.
95 Called by the superclass,
96 you are unlikely to want to call it.
97  
98 =cut
99  
100 24 ▶sub value {
Subroutine value: called CC=2 SCAR=6.9
Pod documented
101 24 my ($self, $value) = @_;
102  
103 24 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 12 12 24
104 12 $self->{value} = $value;
105 }
106  
107 24 return $self->{value};
108 }
109  
110 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
111 4 my $us = CGI::Untaint::CountyStateProvince::US->_new();
112  
113 4 push @CGI::Untaint::CountyStateProvince::countries, $us;
114 };
115  
116 =head1 AUTHOR
117  
118 Nigel Horne, C<< <njh at bandsman.co.uk> >>
119  
120 =head1 BUGS
121  
122 Only two-letter abbreviations are allowable,
123 so 'Mass' won't work for Massachusetts.
124  
125 Please report any bugs or feature requests to C<bug-cgi-untaint-csp-us at rt.cpan.org>, or through
126 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Untaint-CountyStateProvince>.
127 I will be notified,
128 and then you'll automatically be notified of the progress of your bug as I make changes.
129  
130 =head1 SEE ALSO
131  
132 L<CGI::Untaint::CountyStateProvince>, L<CGI::Untaint>, L<Locale::SubCountry>
133  
134 =head1 SUPPORT
135  
136 You can find documentation for this module with the perldoc command.
137  
138 perldoc CGI::Untaint::CountyStateProvince::US
139  
140 You can also look for information at:
141  
142 =over 4
143  
144 =item * RT: CPAN's request tracker
145  
146 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Untaint-CountyStateProvince-US>
147  
148 =item * Search CPAN
149  
150 L<http://search.cpan.org/dist/CGI-Untaint-CountyStateProvince-US>
151  
152 =back
153  
154 =head1 ACKNOWLEDGEMENTS
155  
156 =head1 LICENSE AND COPYRIGHT
157  
158 Copyright 2012-2025 Nigel Horne.
159  
160 This program is released under the following licence: GPL2
161  
162 =cut
163  
164 4 1; # End of CGI::Untaint::CountyStateProvince::US
↑ Summary