on Wed, 29 May 2002 06:55:26 GMT, [EMAIL PROTECTED] (Leila
Lappin) wrote: 

> I need to write a subroutine that validates several fields and
> returns one summary field containing names of what failed. 

An ideal solution would depend on the context of the problem (like 
e.g. whether the fields are coming from a CGI form), which you don't 
supply.

A bare-bones solution could be along the following lines:

    #! perl -w
    use strict;

    my %valid = ( field1 => sub { $_[0] > 0 },
                  field2 => sub { length($_[0])},
                  field3 => sub { $_[0] =~ /^[a..z]+$/}
                );

                      
    sub validator {
        my $data = shift;
        my $valids = shift;
        
        my @result = ();
        
        for my $k (keys %$data) {
            push @result, $k unless $valids->{$k}($data->{$k});
        }
        return @result;
    }

    my %dataset = ( field1 => 123,
                    field2 => 'a',
                    field3 => 'xYz'
                  );

    if (my @r = validator(\%dataset, \%valid)) {
        print "Following fields failed validation: @r\n";
    } else {
        print "All fields validated.\n";
    }

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to