The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Validator::Checker::HOWTO - how to develop checkers for Validator::Var

DESCRIPTION

This HOWTO describes how to develop custom checkers for use with Validator::Var objects. Generally speaking there is no built-in and custom checkers, all of them are simply checkers.

GO

Writing checker

As an example you can look at package Validator::Checker::MostWanted. As you can see - it's very simple. Now let's try to develop your own checkers.

    package Your::Custom::Checker;

    require Exporter;
    our @ISA = qw(Exporter);
    our @EXPORT_OK = qw( Your_Checker Your_Another_Checker );


    sub Your_Checker {[
        \&_your_checker,              # ref to checker subroutine
        'Your_Checker',               # checker name
        'corresponds to Your_Checker' # description
    ]}
    
    sub Your_Another_Checker {[
        \&_your_another_checker,
        'Your_Another_Checker',
        'corresponds to Your_Another_Checker'
    ]};
    
    
    # checker without arguments
    #
    sub _your_checker
    {
        my $var = shift;
        my $bool;
        
        # checkers code
        # ...
        
        return $bool;
    }
    
    # checker with arguments
    #
    sub _your_another_checker
    {
        my ($var, @args) = @_;
        my $bool;
        
        # checkers code
        # ...
        
        return $bool;
    }
    
    
    1;

Using checkers

You can use your newly created checker in standard manner:

    use Your::Custom::Checker qw( Your_Checker Your_Another_Checker );

    my $foo = Validator::Var->new();
    $foo->checker(Your_Checker);
    unless ( $foo->is_valid( $var ) ) {
        warn "variable does not passed Your_Chacker";
    }

    my $bar = Validator::Var->new();
    $bar->checker(Your_Another_Checker, $arg0, $arg1, $arg2 );
    unless ( $bar->is_valid( $var ) ) {
        warn "variable does not passed Your_Another_Checker";
    }

AUTHOR

Fedor Semenov, <fedor.v.semenov at gmail.com>