String::BlackWhiteList - match a string against a blacklist and a whitelist
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;
Using this class you can match strings against a blacklist and a whitelist. The matching algorithm is explained in the valid() method's documentation.
valid()
Constructs a matcher object.
Is an array accessor per Class::Accessor::Complex. Use it to maintain the list of (sub-)strings you consider blacklisted.
Class::Accessor::Complex
Is an array accessor per Class::Accessor::Complex. Use it to maintain the list of (sub-)strings you consider whitelisted.
The actual regular expression (preferably created by qr//) used for blacklist testing.
qr//
The actual regular expression (preferably created by qr//) used for whitelist testing.
Takes the blacklist from blacklist(), generates a regular expression that matches any string in the blacklist and sets the regular expression on black_re().
blacklist()
black_re()
Also takes the whitelist from whitelist(), generates a regular expression that matches any string in the whitelist and sets the regular expression on white_re().
whitelist()
white_re()
If you set a black_re() and a white_re() yourself, you shouldn't use <update(), of course.
update()
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". The Post Drive matches the whitelist, but the P.O. BOX matches the blacklist, so the string is still considered invalid.
P.O. BOX 37, Post Drive 9
Post Drive
P.O. BOX
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.
If you talk about this module in blogs, on del.icio.us or anywhere else, please use the stringblackwhitelist tag.
stringblackwhitelist
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.
bug-string-blackwhitelist@rt.cpan.org
See perlmodinstall for information and options on installing Perl modules.
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/>.
Marcel Grünauer, <marcel@cpan.org>
<marcel@cpan.org>
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.
To install String::BlackWhiteList, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::BlackWhiteList
CPAN shell
perl -MCPAN -e shell install String::BlackWhiteList
For more information on module installation, please visit the detailed CPAN module installation guide.