NAME
String::BlackWhiteList - match a string against a blacklist and a whitelist
SYNOPSIS
use String::BlackWhiteList;
use constant BLACKLIST => (
'POST',
'PO',
'P O',
'P O BOX',
'P.O.',
'P.O.B.',
'P.O.BOX',
'P.O. BOX',
'P. O.',
'P. O.BOX',
'P. O. BOX',
'POBOX',
);
use constant WHITELIST => (
'Post Road',
'Post Rd',
'Post Street',
'Post St',
'Post Avenue',
'Post Av',
'Post Alley',
'Post Drive',
);
my @ok = (
'Post Road 123',
'Post Rd 123',
'Post Street 123',
'Post St 123',
'Post Avenue 123',
);
my @not_ok = (
'Post',
'P.O. BOX 37',
'P.O. BOX 37, Post Drive 9',
'Post Street, P.O.B.',
);
plan tests => @ok + @not_ok;
my $matcher = String::BlackWhiteList->new(
blacklist => [ BLACKLIST ],
whitelist => [ WHITELIST ]
)->update;
ok( $matcher->valid($_), "[$_] valid") for @ok;
ok(!$matcher->valid($_), "[$_] invalid") for @not_ok;
DESCRIPTION
Using this class you can match strings against a blacklist and a whitelist. The matching algorithm is explained in the valid()
method's documentation.
METHODS
- new
-
Constructs a matcher object.
- blacklist
-
Is an array accessor per
Class::Accessor::Complex
. Use it to maintain the list of (sub-)strings you consider blacklisted. - whitelist
-
Is an array accessor per
Class::Accessor::Complex
. Use it to maintain the list of (sub-)strings you consider whitelisted. - black_re
-
The actual regular expression (preferably created by
qr//
) used for blacklist testing. - white_re
-
The actual regular expression (preferably created by
qr//
) used for whitelist testing. - update
-
Takes the blacklist from
blacklist()
, generates a regular expression that matches any string in the blacklist and sets the regular expression onblack_re()
.Also takes the whitelist from
whitelist()
, generates a regular expression that matches any string in the whitelist and sets the regular expression onwhite_re()
.If you set a
black_re()
and awhite_re()
yourself, you shouldn't use <update()
, of course. - valid
-
Takes a string and tries to determine whether it is valid according to the blacklist and the whitelist. This is the algorithm used to determine validity:
If the string matches the whitelist, then the part of the string that didn't match the whitelist is checked against the blacklist. If the remainder matches the blacklist, the string is still considered invalid. If not, it is considered valid.
Consider the example of
P.O. BOX 37, Post Drive 9
in the "SYNOPSIS". ThePost Drive
matches the whitelist, but theP.O. BOX
matches the blacklist, so the string is still considered invalid.If the string doesn't match the whitelist, but it matches the blacklist, then it is considered invalid.
If the string matches neither the whitelist nor the blacklist, it is considered valid.
Undefined values and empty strings are considered valid. This may seem strange, but there is no indication that they are invalid and in dubio pro reo.
TAGS
If you talk about this module in blogs, on del.icio.us or anywhere else, please use the stringblackwhitelist
tag.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-string-blackwhitelist@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
INSTALLATION
See perlmodinstall for information and options on installing Perl modules.
AVAILABILITY
The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN site near you. Or see <http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>.
AUTHOR
Marcel Grünauer, <marcel@cpan.org>
COPYRIGHT AND LICENSE
Copyright 2007 by Marcel Grünauer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.