1

我有一个看起来像这样的 CGI 脚本。

#!/usr/local/bin/perl

use CGI ':standard';

print header;
print start_html('A Simple Example'),
h1('A Simple Example'),
start_form,
"What's your name? ",textfield('name'),
p, submit, end_form,
hr;

my %unwantedwords = {'foo' => 1 };

if (param())
{
    my $text =param('name');

    # I attempted this to but failed.
    unless ($unwantedwords{$text}){
        print  "Your name is: ",$text,
   }
    hr;
}
print
end_html;

我想做的基本上是通过'textfield'接收文本,然后在网上打印出来。但是,当用户插入的单词是不需要的单词(存储在哈希中)时,我希望网络返回到它的新初始状态,而不是打印它。

最好的方法是什么?上面的代码不起作用。

4

2 回答 2

1

像,(未经测试)..

use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );

my @unwanted = qw( foo bar baz );

my $text = param('name');

print header,
      start_html('A Simple Example');

display_form() and exit unless !grep($text eq $_, @unwanted);

print "Hello $text\n";

sub display_form {
   print start_form,
         h1('A Simple Example'),
         qq( What's your name? ), textfield(-name => 'name', -value => '', -override => 1), p,
         submit, hr,
         end_form;
}

print end_html;
于 2013-06-11T02:01:25.460 回答
0

您需要在提交之前保存单词的状态,如果提交的单词在您的坏词列表中,则检索它以发回。

您的实现将根据您的持久性引擎而有所不同,但无论您是使用 cookie 存储旧词还是使用会话存储,您都将执行以下操作:

 1. store the old word

 2. send the old word along with the web form.

 3. receive new word back

 4. if (new word is in the bad word list) {
      get the old word from storage
    }
    else {
      store the new word
    }

 5. do what comes next
于 2013-06-10T14:27:51.783 回答