View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to > String-BlackWhiteList > String::BlackWhiteList



Annotate this POD


View/Report Bugs
Module Version: 1.100860   Source  


String::BlackWhiteList - Match a string against a blacklist and a whitelist


version 1.100860


    use String::BlackWhiteList;
    use Test::More;

    use constant BLACKLIST => (
        'P O',
        'P O BOX',
        'P.O. BOX',
        'P. O.',
        'P. O.BOX',
        'P. O. BOX',

    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 = (
        '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 ]

    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.



The actual regular expression (preferably created by qr//) used for blacklist testing.


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().

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().

The individual entries of blacklist() and whitelist() are assumed to be regular expressions. If you have some regular expressions and some literal strings, you can use \Q...\E. If all your strings are literal strings, set is_literal_text().

If you set a black_re() and a white_re() yourself, you shouldn't use <update(), of course.


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.

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 when in doubt, trust them.


Like valid(), but once a string passes the whitelist, it is not checked against the blacklist anymore. That is, if a string matches the whitelist, it is valid. If not, it is checked against the blacklist - if it matches, it is invalid. If it matches neither whitelist nor blacklist, it is valid.


See perlmodinstall for information and options on installing Perl modules.


No bugs have been reported.

Please report any bugs or feature requests through the web interface at


The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit to find a CPAN site near you, or see

The development version lives at Instead of sending patches, please fork this project using the standard git and github infrastructure.


  Marcel Gruenauer <>


This software is copyright (c) 2005 by Marcel Gruenauer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: