Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Reason for Creation

In a current project we required a way of accessing data by state based on regions and sub regions (referred to as divisions) as described by the Census Bureau. I did a quick search on CPAN and didn't find a regions specific module, I found several that were State based however and leverage one for this module.

Initial Solution Requirements

  1. Region breakdown of states as outlined by the US Census
  2. Divisions within the Regions as outlined by the US Census
  3. Ability to return each of the above in a list, order not important
  4. Ability to find all states with in each of the above
  5. Have the state list returned in alphabetical order
  6. Have the state list returned in either "full name" or abbreviated form ( South Dakota -or- SD )

Module Code:

package Region; =pod Module purpose is to provide access to US regions, divisions, and stat +es grouped either by the above or individually. See inline comments for more information. =cut use strict; use Geography::States; my $debug = 0; my %regions; my $region; my $division; my $gs = Geography::States->new('USA'); while (<DATA>) { next if $_ !~ m/[a-zA-Z]/ || $_ =~ /^#/; if (m/^[A-Z]/ && m/[A-Z]$/) { $_ =~ s/\s+$//; print $_ , "\n" if $debug; $region = ucfirst(lc($_)); } elsif (m/^[A-Z]/ && m/[a-z]$/) { $_ =~ s/\s+$//; print "\t$_\n" if $debug; $division = $_; } elsif (m/^\s+\w/) { $_ =~ s/^\s+|\s+$//g; my $code = $gs->state($_); push @{ $regions{$region}{$division} } , { full => $_ , code => $c +ode } ; } } sub new { my $class = shift; my $self = {}; $self = \%regions; bless $self, $class; } =pod The regions method will return a list of regions in alphabetical order =cut sub regions { my $self = shift; return sort keys %{ $self }; } =pod The divisions method will return a list of all of the divisions within a region. It accepts a list of arguments, those items must be equal to the region names. When a list is passed only the divisions in the regions passed will be returned. =cut sub divisions { my ($self,@reg) = @_; my @list; if (!$reg[0]) { @reg = sort keys %{ $self }; } @reg = map { ucfirst(lc($_)) } @reg; foreach my $region (@reg) { foreach my $division (sort keys %{ $self->{$region} }) { push @list, $division; } } return @list; } =pod The state method will return an array of states, the contents of which are determined by arguments passed to the method. If no options (hash) is sent in then it will return a list of all the state codes in alphabetical order. The state name can be returned if key "name" has value of 'full' States for a region can be returned if an option of 'region' has been set to one of the available regions. States for a division can be returned if an option of 'division' has b +een set to one of the available divisions. The only mixing that can be done is State name type (full or code) alo +ng with division OR state. Sending both a region and division will only w +ork if the division selected is under the region selected. =cut sub state { my ($self,%args) = @_; my $verbiage = $args{name} || 'code'; my $region_ = lc($args{region}) || 'ALL'; my $division_ = lc($args{division}) || 'ALL'; my @list; foreach my $region (keys %{ $self }) { next if $region_ ne 'ALL' && lc($region) ne $region_; foreach my $division (keys %{ $self->{$region} }) { next if $division_ ne 'ALL' && lc($division) ne $division_; foreach my $state ( @{ $self->{$region}{$division} } ) { push @list , $state->{$verbiage}; } } } return sort(@list); } __DATA__ NORTHEAST Middle Atlantic New Jersey New York Pennsylvania New England Connecticut Maine Massachusetts New Hampshire Rhode Island Vermont MIDWEST East North Central Illinois Indiana Michigan Ohio Wisconsin West North Central Iowa Kansas Minnesota Missouri Nebraska North Dakota South Dakota SOUTH East South Central Alabama Kentucky Mississippi Tennessee South Atlantic Delaware District of Columbia Florida Georgia Maryland North Carolina South Carolina Virginia West Virginia West South Central Arkansas Louisiana Oklahoma Texas WEST Mountain Arizona Colorado Idaho Montana Nevada New Mexico Utah Wyoming Pacific Alaska California Hawaii Oregon Washington #POSSESSIONS # # Puerto Rico # Virgin Islands # Pacific Islands # # Pacific Islands Includes: Canton, Guam, Mariana, Marshall, Samoa, Wa +ke

Informal Test Code

#!/usr/bin/perl use Region; use strict; my $regions = Region->new(); print join("\n",$regions->regions); print "\n\n"; print join("\n",$regions->divisions); print "\n\n"; print join("\n",$regions->divisions('west')); print "\n\n"; print join("\n",$regions->state( name => 'full' , region => 'west' ) ) +; print "\n\n"; print join("\n",$regions->state( name => 'full' , division => 'East No +rth Central' ) ); print "\n\n"; print join("\n",$regions->state( name => 'code' , region => 'South' , +division => 'South Atlantic' ) ); print "\n\n";

Possible Module Names

Geography::US::Census::Regions
Geography::US::Regions::Census
Locale::US::Census::Regions
???

Interest/Comments

Is there any interest in this module for addition to CPAN or is there an existing module that I overlooked that already fills this space?


General comments on design and method interfaces would be appreciated even if you don't need or want the module.

UPDATED: Moved the while loop outside of the new to avoid issues if the user attempted to create multiple objects within a single script.

Removed the 'use Data::Dumper' that was left over from initial testing.

In reply to RFC US Region Module by trs80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2020-07-06 11:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?