sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

While building a meta tag script everything was going fine until I decided to add some extra tests. Now I'm confused on how to print the HTML section and errors while doing multiple tests.

A chunk of code:

if ($char_keywords > 950) { print qq(<td><font color="red">Keywords: </font></td>); print qq(<td><font color="red">You had $char_keywords chars. +Max is 950.</font><br>); } else { print qq(<td>Keywords: </td>); }
Right now, it's only checking the char count. Simple enough. But now I want to also check the number of times each keyword was used. I already have this test setup but I don't know how I can combine both of these together.

It's an HTML form where if the data you input is invalid, the form is reloaded with the errors above text field which they "goofed". As you can see, when the char test fails it colors KEYWORDS red notifying you goofed. I can't add the repeating test inside the keyword test because they're two separate tests entirely. And I can't have them one after another because I can only print the word "kewords" once in red (I don't want to print the word twice if both tests fail).

Somehow these tests need to work together to only print out KEYWORDS in red once but yet both act as separate tests.

My brain hurts, any ideas on how to go about doing this?

The other test (which tests word repeats) is this:

foreach (keys %one_word_keywords) { my $key = $_; my $value = $one_word_keywords{$_}; if ($value > 6) { # this has to be printed in the normal table as well # but if this error happens AND the chars does # the word KEYWORDS would be colored red twice. print qq("$key" was repeated $value times. Max is 6<br><br>); } }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Multiple testing methods
by JediWizard (Deacon) on Sep 26, 2004 at 19:18 UTC

    I think what you need here is an error flag. Do both tests, and if either fails, set the error falg to a true value, then do your print statments based on the value in the error flag. See Below:

    my $error = 0; my $message = ''; if($char_keywords > 950){ $error++; $message = "You had $char_keywords chars. Max is 950"; } if(&failrepeattest()){ $error++; $message .= '<br />' if(length($message)); $message .= 'Repeated keyowrd error message.'; } if($error){ print qq(<td><font color="red">Keywords: </font></td>); print qq(<td><font color="red">$message</font><br>); }else{ print qq(<td>Keywords: </td>); }
    May the Force be with you
      I'd go with something similar to the above, but remove some stuff:
      my $message = ''; if ( $char_keywords > 950 ) { $message = "You had $char_keywords chars. Max is 950"; } if ( failrepeattest() ) { $message .= '<br />' if $message; $message .= 'Repeated keyword error message.'; } if ( $message ) { print qq(<td><font color="red">Keywords: </font></td>); print qq(<td><font color="red">$message</font><br>); } else { print qq(<td>Keywords: </td>); }
      You don't need the $error flag. The $message will be empty (false) if there are no errors. I've also taken out the length() check on $message for the same reason .. if the message has length then testing it in this context will return true and the <br> tag will be output.

      Cheers!
      Rick
      If this is a root node: Before responding, please ensure your clue bit is set.
      If this is a reply: This is a discussion group, not a helpdesk ... If the discussion happens to answer a question you've asked, that's incidental.
Re: Multiple testing methods
by pg (Canon) on Sep 26, 2004 at 18:59 UTC

    Not clear exactly what your requirement is, but it would be something like this:

    if (faillengthtest($foo)) { if (failrepeattest($foo)) { #format the page with error msg 1 } else { #format the page with error msg 2 } } else { #format the page normally }

    Might be a good idea to wrap your testing in two functions to make the code clearly expressed.

    Update:

    Actually to exercise all 4 combinations, the code should be (obviously you could tailer some branches, if they don't appear to be a real combination under your requirement):

    if (faillengthtest($foo)) { if (failrepeattest($foo)) { #format the page with error msg 1 } else { #format the page with error msg 2 } } else { if (failrepeattest($foo)) { #format the page with error msg 3 } else { #format the page with normal message } }
      The problem with this code is one test is nested within the other. This means if the faillength test doesn't fail, there's no possible way for the repeat test to fail either.


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
CGI::QuickForm might be a nice option
by dazzle (Sexton) on Sep 26, 2004 at 23:33 UTC
    You didn't say that using another Perl module was out of the question, so how about taking a look at CGI::QuickForm? If you create the form and supply the validation criteria in the form of a subroutine, it will automatically validate and highlight the error fields for you. Here's a snippet from the CGI::QuickForm perldoc:

    -VALIDATE

    Optional subroutine reference. This routine is called after each individual field has been validated. It is given the fields in a name=>value hash. It should either return a simple true (valid) or false (invalid) or a two element list, the first element being a true/false value and the second value either an empty string or an (html) string which gives the reason why the record is invalid. Typically it may have this structure:
    sub valid_record { my %field = @_ ; my $valid = 1 ; my $why = '' ; # Do some multi-field validation, e.g. if( $field{'colour'} eq 'blue' and $field{'make'} eq 'estate' ) { $valid = 0 ; # No blue estates available. $why = '<b><i>No blue estates available</i></b>' ; } # etc. ( $valid, $why ) ; }