GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Badger::Filter(3) User Contributed Perl Documentation Badger::Filter(3)

Badger::Filter - object for simple filtering

    use Badger::Filter;
    
    # filter to only include certain things
    my $filter = Badger::Filter->new(
        include => [
            'meat',
            qr/beer|wine/,
            sub { $_[0] eq 'soda' },
        ],
    );

    # filter to only exclude certain things
    my $filter = Badger::Filter->new(
        exclude => [
            'cheese',
            qr/alcohol-free|salad|diet/,
            sub { $_[0] eq 'diet soda' },
        ],
    );

    # filter to include and exclude
    my $filter = Badger::Filter->new(
        include => [
            ...
        ],
        exclude => [
            ...
        ],
    );

    # filtering items
    my @items = $filter->accept(
        'meat', 'cheese', 'potato salad', 'green salad',
        'beer', 'alcohol-free beer',
        'wine', 'soda', 'diet soda'
    );

This module defines a simple object for filtering data. Items can be included and/or excluded via a simple set of rules.

    use Badger::Filter;
    
    my $filter = Badger::Filter->new(
        include => [
            # inclusion rules
        ],
        exclude => [
            # exclusion rules
        ],
    );

The rules that can be specified in either include or exclude options can be simple strings, regular expressions, hash or list references.

    my $filter = Badger::Filter->new(
        include => [
            'meat',                     # fixed string
            qr/beer|wine/,              # regular expression
            sub { $_[0] eq 'soda' },    # subroutine,
            {
                foo => 1,               # 'foo' fixed string
                bar => 1,               # 'bar' fixed string
                baz => 0,               # ignored (not a true value)
            },
        ],
    );

The accept() method returns any items that match any of the include rules and don't match any exclude rules.

    my @matching = $filter->accept(@candidates);

If there are any include rules, then a candidate item must match at least one of them to be included. If there aren't any include rules then the candidate is assumed to be included by default.

All candidates that are included are then filtered through the exclude rules. If a candidate matches an exclude rule then it is rejected. If it doesn't match an exclude rule, or there aren't any exclude rules defined then the candidate item is accepted.

One or more items that should be included (i.e. not filtered out) by the filter. This can be any of:
  • A simple string. This should match a candidate string exactly for it to be included.
  • A string containing a '*' wilcard character, e.g. "foo/*". The star is used to represent any sequence of characters.
  • A regular expression. This should match a candidate string for it to be included.
  • A code reference. The function will be called, passing the candidate as the only argument. It should return any TRUE value if the item should be included or false if not.
  • A hash reference. All keys in the hash reference with corresponding values set to any TRUE value are considered to be simple, static strings that a candidate string should match.
  • A list reference. Containing any of the above.

One or more items that should be excluded (i.e. filtered out) by the filter. This can be any of the same argument types as for include.

Constructor method.

    my $filter = Badger::Filter->new(
        include => ['badger'],
        exclude => ['ferret'],
    );

Returns a list (in list context) or reference to a list (in scalar context) of all items passed as arguments that are accepted (passed through) by the filter. Each item is tested via a call to item_accepted().

Returns a list (in list context) or reference to a list (in scalar context) of all items passed as arguments that are rejected by the filter. Each item is tested via a call to item_rejected()).

Return true if the item is included (via a call to the item_included() method) and not excluded (ditto to item_excluded).

    my $accept = $filter->item_accepted($candidate);

Return the logical opposite of accepted().

    my $reject = $filter->item_rejected($candidate);

Tests if $item should be included by the filter (i.e. passed through, NOT filtered out).

If there is no include specification defined then the item is accepted by default (return value is TRUE).

If there is an include specification then the item must match any one of the include checks for it to be included (returns TRUE), otherwise it is rejected (returns FALSE).

Tests if $item should be excluded by the filter (i.e. NOT passed through, filtered out).

If there is no exclude specification defined then the item is accepted by default. In this case, the return value is FALSE (i.e. item is NOT excluded, thus it is included).

If there is an exclude specification then the item will be excluded (return value is TRUE) if it matches any of the exclude checks. Otherwise the method returns FALSE to indicate that the item is NOT excluded (i.e. included).

Andy Wardley <http://wardley.org/>

Copyright (C) 2013 Andy Wardley. All Rights Reserved.

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

2016-12-12 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.