Business-Address-POBox

 view release on metacpan or  search on metacpan

lib/Business/Address/POBox.pm  view on Meta::CPAN

use 5.008;
use strict;
use warnings;

package Business::Address::POBox;
BEGIN {
  $Business::Address::POBox::VERSION = '1.101230';
}

# ABSTRACT: Check whether an address looks like a P.O.Box
use String::BlackWhiteList;
use parent qw(Class::Accessor::Complex Class::Accessor::Constructor);
__PACKAGE__->mk_constructor->mk_object_accessors(
    'String::BlackWhiteList' => 'matcher')
  ->mk_array_accessors(qw(blacklist whitelist));
use constant DEFAULTS => (
    blacklist => [
        '\b(BOX|POB|POST(BOX|SCHACHTEL|FACH|LAGERND|BUS)?|POBOX)\b',
        '\b(P\.?\s*O\.?(\s*B(\.|OX))?)\b',
        '(^|\b)P\.?F\.?(-|\s+)\d',
    ],
    whitelist => [
        'Pf(-|\s+)\D',
'\b((Alte|An\s+der(\s+alten)?)\s+Post|Post(-|\s+)(Road|Rd|Street|St|Avenue|Av|Alley|Drive|Grove|Walk|Parkway|Row|Lane|Bridge|Boulevard|Square|Garden|Strasse|Gasse|Allee|Platz))\b',
    ],
);

sub init {
    my $self = shift;
    $self->update;
}

sub update {
    my $self = shift;
    for ($self->matcher) {
        $_->blacklist($self->blacklist);
        $_->whitelist($self->whitelist);
        $_->update;
    }
}

sub is_pobox {
    my ($self, $text) = @_;
    return 0 if $self->matcher->valid($text);
    my $black_re = $self->matcher->black_re;
    # The documentation below explains this mess.
    $text =~ s/$black_re//gi;
    $text =~ s/[^\sa-z]//gi;
    $text =~ s/^\s+|\s+$//g;
    for my $word (split /\s+/, $text) {
        return 0 if length($word) > 1;
    }
    return 1;
}

sub is_pobox_relaxed {
    my ($self, $text) = @_;
    !$self->matcher->valid_relaxed($text);
}
1;


__END__
=pod

=head1 NAME

Business::Address::POBox - Check whether an address looks like a P.O.Box

=head1 VERSION

version 1.101230

=head1 SYNOPSIS

    use Business::Address::POBox;

    my $address = 'Universitaetsstrasse 7, PF 34';
    if (Business::Address::POBox->new->is_pobox($address)) {
        # do something with the address
    }

=head1 DESCRIPTION

This class tries to determine whether or not an string refers to a P.O. box.
This is sometimes relevant if your business process, for legal reasons, needs
a real address and not a P.O. box. Actually, it needs to be a deliverable
address. So a real address that happens to have a P.O. box is ok too.

It has predefined blacklists and whitelists that should catch most English and
German P.O. box addresses, but you can modify these lists with the methods
provided. Note that the entries are literal strings, not regular expressions.



( run in 0.685 second using v1.01-cache-2.11-cpan-7fcb06a456a )