0

我正在尝试编写一个 perl 脚本,它允许编辑单独的问题和答案文件。如果不编写看起来很荒谬、效率低下的代码,我似乎无法找出似乎可以正常工作的东西。

我希望它能够向文件添加全新的问题/答案,以及编辑已经存在的问题/答案。关于如何使这项工作的任何建议?

编辑:假设我有一个文本区域。我可以制作一个按钮,从相关文件中删除在 textarea 中输入的文本吗?一个文本区域,当您输入问题时,您可以选择按“删除”按钮,如果该问题存在于 .txt 文件中,是否将其从文件中删除?

这是脚本:

if($newquestion != $oldquestion and $newanswer != $oldanswer) {
            print $ANS "$newquestion\t$newanswer\n";
        } else {
            if($newquestion != $oldquestion and $newanswer == $oldanswer) {
                print $ANS "$newquestion\t$newanswer\n";
            } elsif($newquestion == $oldquestion and $newanswer != $oldanswer) {
                print $ANS "$oldquestion\t$newquestion\n";
            }   
        }
    }   

这是html:

<html>
    <body>
        Edit Questions and Answers!<br><br>
        Type the question in the first area, and the answer to it in the other<br>
        ~answerlist~<br><br>
        <form action="viewquestions.dhtml" method="post">
            Questions and Answers:<br>
            <textarea rows="1" cols="25" name="newquestion">Question
            </textarea>
            <textarea rows="1" cols="25" name="newanswer">Answer
            </textarea>
            <input type="submit" value="submit"/>
            <input type="hidden" name="site" value="~site~"/>
            <input type="hidden" name="xs" value="~xs~"/>
            <input type="hidden" name="username" value="~username~"/>
        </form>
        <a href="/client_homepage.dhtml~useridtext~&frompage=View and Answer Questions">Return to home page</a><br>
    </body>
</html>
4

1 回答 1

0

你因为缺乏基础而产生了糟糕的代码,买一两本关于初学者 Web 编程的书

您将问题/答案放入数据库而不是文本文件。这保证了在写入多个并发数据集时不会丢失更新。

echo 'create table qa(id integer primary key autoincrement, question text, answer text);' | sqlite3 qa.sqlite

为方便起见,使用 ORM 访问数据库(第 3-5 行)。您的 Web 应用程序需要一个单页“猴子表单”:

use 5.010;
use strictures;
use DBIx::Class::Schema::Loader qw();
DBIx::Class::Schema::Loader->naming({ALL => 'preserve'});
my $table = DBIx::Class::Schema::Loader->connect('dbi:SQLite:dbname=qa.sqlite')->resultset('Qa');

use Text::Xslate qw();
my $tt = Text::Xslate->new(syntax => 'TTerse');
my $template = <<'';
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Edit Questions and Answers!</title>
        <style>
            textarea { width: 25em; height: 1em; }
        </style>
    </head>
    <body>
        <h1>existing questions and answers</h1>
        [% FOR pair IN qa %]
        <form action="[% action %]" method="POST">
            <input type="hidden" name="id" value="[% pair.id %]" />
            <textarea name="question">[% pair.question %]</textarea>
            <textarea name="answer">[% pair.answer %]</textarea>
            <input type="submit" name="update" value="update" />
            <input type="submit" name="delete" value="delete" />
        </form>
        [% END %]
        <h1>new question and answer</h1>
        <form action="[% action %]" method="POST">
            <label for="question">Question:</label>
            <textarea name="question" id="question"></textarea>
            <label for="answer">Answer:</label>
            <textarea name="answer"></textarea>
            <input type="submit" name="create" value="create" />
        </form>
    </body>
</html>

use Plack::Request qw();
my $app = sub {
    my $req = Plack::Request->new(shift);
    my $op = delete $req->body_parameters->{delete} //
             delete $req->body_parameters->{update} //
             delete $req->body_parameters->{create};
    if ('POST' eq $req->method) {
        if ('delete' eq $op) {
            $table->find($req->body_parameters->{id})->delete;
        } else {
            $table->update_or_create($req->body_parameters->as_hashref);
        }
    }
    return $req->new_response(
        200,
        ['Content-Type' => 'application/xhtml+xml'],
        [$tt->render_string($template, { action => $req->uri, qa => [$table->all] })]
    )->finalize;
};

这就是整个代码。使用 运行程序plackup

于 2013-07-09T12:46:32.577 回答