File Coverage

blib/lib/CGI/Untaint/CountyStateProvince/US.pm
Criterion Covered Total %
statement 35 36 97.2
branch 12 14 85.7
condition 4 6 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 60 65 92.3


line stmt bran cond sub pod time code
1             package CGI::Untaint::CountyStateProvince::US;
2              
3 4     4   1556261 use warnings;
  4         12  
  4         304  
4 4     4   25 use strict;
  4         10  
  4         124  
5 4     4   2698 use Locale::SubCountry;
  4         484532  
  4         2198  
6              
7             # use base qw(CGI::Untaint::object CGI::Untaint::CountyStateProvince);
8 4     4   54 use base 'CGI::Untaint::object';
  4         15  
  4         2923  
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             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             sub is_valid {
56 12     12 1 52 my $self = shift;
57              
58 12         35 my $value = uc($self->value());
59 12         75 $value =~ s/^\s+|\s+$//g; # Trim whitespace
60              
61 12 100       63 if($value =~ /([A-Z][A-Z\s]+)/) {
62 11         33 $value = $1;
63             } else {
64 1         3 return 0;
65             }
66              
67 11 100       38 unless($self->{_validator}) {
68 2         24 $self->{_validator} = Locale::SubCountry->new('US');
69 2 50       104 unless($self->{_validator}) {
70 0         0 return 0;
71             }
72             }
73              
74 11         45 my $state = $self->{_validator}->code($value);
75 11 100 66     3712 if($state && ($state ne 'unknown')) {
76             # Given full state name
77             # Detaintify
78 3 50       24 if($state =~ /(^[A-Z]{2}$)/) {
79 3         18 return $1;
80             }
81             }
82              
83 8         32 $state = $self->{_validator}->full_name($value);
84 8 100 66     272 if($state && ($state ne 'unknown')) {
85             # Given two-letter abbreviation
86 4         16 return $value;
87             }
88              
89 4         13 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             sub value {
101 24     24 1 421127 my ($self, $value) = @_;
102              
103 24 100       67 if(defined($value)) {
104 12         37 $self->{value} = $value;
105             }
106              
107 24         66 return $self->{value};
108             }
109              
110             BEGIN {
111 4     4   3293 my $us = CGI::Untaint::CountyStateProvince::US->_new();
112              
113 4         290 push @CGI::Untaint::CountyStateProvince::countries, $us;
114             };
115              
116             =head1 AUTHOR
117              
118             Nigel Horne, C<< >>
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, or through
126             the web interface at L.
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, L, L
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
147              
148             =item * Search CPAN
149              
150             L
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             1; # End of CGI::Untaint::CountyStateProvince::US