File Coverage

blib/lib/Locale/BR.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Locale::BR;
2              
3 3     3   68197 use warnings;
  3         10  
  3         113  
4 3     3   20 use strict;
  3         8  
  3         133  
5              
6 3     3   15 use base 'Exporter';
  3         9  
  3         1345  
7             our @EXPORT_OK = qw(code2state state2code all_state_codes all_state_names);
8             our %EXPORT_TAGS = (
9             all => [@EXPORT_OK],
10             );
11              
12             =encoding utf8
13              
14             =head1 NAME
15              
16             Locale::BR - Identify Brazilian states by two-letter codes and vice-versa
17              
18             =cut
19              
20             our $VERSION = '0.02';
21              
22             my %code_for_state = ();
23             my %state_for_code = ();
24              
25             =head1 SYNOPSIS
26              
27             use Locale::BR qw( :all );
28              
29             my $state = code2state('RJ'); # 'Rio de Janeiro'
30             my $code = state2code('Amapá'); # 'AM'
31            
32             my @states = all_state_names();
33             my @codes = all_state_codes();
34              
35             =head1 EXPORT
36              
37             This modules exports nothing by default, so you'll have to use the ':all'
38             export tag in order to import the subroutines to your namespace. Or you can
39             explicitly name any of the following subroutines:
40              
41             =head2 code2state
42              
43             Takes a state code and returns a string containing the name of the state. If
44             the code is not a valid state code, returns C.
45              
46             This subroutine is case insensitive. For instance, 'mg', 'Mg', 'MG' and even
47             'mG' will all return 'Minas Gerais'.
48              
49             =cut
50              
51             sub code2state {
52 2704     2704 1 5704602 my $code = uc shift;
53            
54 2704         13882 return $state_for_code{$code};
55             }
56              
57             =head2 state2code
58              
59             Takes a state name and returns a string containing its respective code. If the
60             name is not a valid state name, returns C.
61              
62             This subroutine is case insensitive and understands state names with and
63             without accentuation. For instance, 'Amapá', 'amapá', 'Amapa' and 'amapa' will
64             all return 'AP'.
65              
66             =cut
67              
68             sub state2code {
69 114     114 1 40401 my $state = uc shift;
70            
71 114         445 return $code_for_state{$state};
72             }
73              
74             =head2 all_state_names
75              
76             Returns an alphabetically ordered list of all Brazilian state names.
77              
78             =cut
79              
80             sub all_state_names {
81 1     1 1 22953 return sort values %state_for_code;
82             }
83              
84             =head2 all_state_codes
85              
86             Returns an alphabetically ordered list of all Brazilian state codes.
87              
88             =cut
89              
90             sub all_state_codes {
91 1     1 1 324 return sort keys %state_for_code;
92             }
93              
94             =head1 SEE ALSO
95              
96             L<< Locale::Country >>
97              
98             L<< Locale::US >>
99              
100              
101             =head1 AUTHOR
102              
103             Breno G. de Oliveira, C<< >>
104              
105             =head1 BUGS
106              
107             Please report any bugs or feature requests to C, or through
108             the web interface at L. I will be notified, and then you'll
109             automatically be notified of progress on your bug as I make changes.
110              
111              
112              
113             =head1 SUPPORT
114              
115             You can find documentation for this module with the perldoc command.
116              
117             perldoc Locale::BR
118              
119              
120             You can also look for information at:
121              
122             =over 4
123              
124             =item * RT: CPAN's request tracker
125              
126             L
127              
128             =item * AnnoCPAN: Annotated CPAN documentation
129              
130             L
131              
132             =item * CPAN Ratings
133              
134             L
135              
136             =item * Search CPAN
137              
138             L
139              
140             =back
141              
142              
143             =head1 LICENSE AND COPYRIGHT
144              
145             Copyright 2009 Breno G. de Oliveira.
146              
147             This program is free software; you can redistribute it and/or modify it
148             under the terms of either: the GNU General Public License as published
149             by the Free Software Foundation; or the Artistic License.
150              
151             See http://dev.perl.org/licenses/ for more information.
152              
153              
154             =cut
155              
156             {
157             # initialization code
158             local $_;
159            
160             while () {
161             next unless /\S/;
162             chomp;
163             my ($code, $state, $state_alt) = split /:/;
164              
165             $state_for_code{$code} = $state;
166            
167             $code_for_state{uc $state} = $code;
168             if ($state_alt) {
169             $code_for_state{uc $state_alt} = $code;
170             }
171             }
172             close DATA;
173             }
174              
175              
176             42; # End of Locale::BR
177              
178             __DATA__