Steve Bertrand wrote:
I'm currently working on a field value validation system. It takes in a
custom data type, and then runs each key through a switch statement and
validates the value.

As I add new data types, I run the type through the switch, to ensure
all fields are tested. When they are not, I classify them within the
appropriate case statement. For example:

# STRINGS
case /^(comment|plan_type|description|desc|started|
        billing_address1|billing_address2|shipping_address1|
        shipping_address2|billing_company_name|shipping_company_name|
        shipping_town|billing_town)$/x

        {
             if ($self->safe_string($value)) {
                 $error->add_message( "$attribute contains bad char " .
                        $self->safe_string($value)
                 );
             }
}

This is becoming extremely boring and tedious, as I have eight or nine
similar but different case statements. I haven't been able to figure out
a way to do this that will save me any time or work. Any suggestions on
how I can classify the keys in a better (ie less boring and eye popping)
way?

The first thing I would say is that you shouldn't use the Switch module. It can cause erratic and hard to find bugs in your code.

You don't show enough code to make good diagnosis but you may be better served by using a dispatch table instead of regular expressions in a switch block.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to