简体   繁体   English

使用perl和html编辑问题和答案文件

[英]Edit a file of questions and answers with perl and html

I am trying to write a perl script which allows the editing of a separate file of questions and answers. 我正在尝试编写一个perl脚本,该脚本允许编辑问题和答案的单独文件。 I can't seem to figure out something that seems to work well without writing ridiculous looking, inefficient code. 如果不编写看起来可笑,效率低下的代码,我似乎无法弄清楚什么似乎行之有效。

I want it to be able to add completely new questions/answers to the file, as well as edit already existing questions/answers. 我希望它能够向文件添加全新的问题/答案,以及编辑已经存在的问题/答案。 Any advice on how to make this work? 关于如何进行这项工作的任何建议?

EDIT: Say I have a textarea. 编辑:说我有一个textarea。 Can I make a button that deletes the text entered in the textarea from the related file? 我可以创建一个按钮来从相关文件中删除在textarea中输入的文本吗? A textarea that, when you enter in a question, you can choose to press the "delete" button, and, if that question exists inside of the .txt file, remove it from the file? 一个文本区域,当您输入问题时,可以选择按“删除”按钮,如果该问题存在于.txt文件中,请从文件中删除它?

This is the script: 这是脚本:

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";
            }   
        }
    }   

This is the html: 这是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>

You produce that bad code because you lack the fundamentals, get a book or two about beginner Web programming . 因为缺乏基础知识,所以您产生了不好的代码,因此, 有一两本关于初学者Web编程的书

You put your questions/answer into a database instead of a text file. 您将问题/答案放入数据库而不是文本文件中。 This guarantees that updates are not lost when several concurrent datasets are written. 这样可以保证在写入多个并发数据集时更新不会丢失。

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

Access the database with an ORM for convenience (lines 3-5). 为了方便起见,使用ORM访问数据库(第3-5行)。 Your Web application calls for a single-page "monkey form": 您的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;
};

That's the whole code. 这就是整个代码。 Run the program with plackup . 使用plackup运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM