我正在尝试编写一个perl脚本,允许编辑单独的问题和答案文件。我似乎无法找到一些似乎运作良好的东西,而无需编写荒谬的外观,效率低下的代码。
我希望它能够为文件添加全新的问题/答案,以及编辑已存在的问题/答案。关于如何使这项工作的任何建议?
编辑:说我有一个textarea。我可以创建一个按钮来删除相关文件中textarea中输入的文本吗?一个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>
答案 0 :(得分:0)
由于缺乏基础知识get a book or two about beginner Web programming,因此产生了错误的代码。
您将问题/答案放入数据库而不是文本文件中。这可以保证在写入多个并发数据集时不会丢失更新。
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
运行程序。