[英]Edit a file of questions and answers with perl and html
我正在嘗試編寫一個perl腳本,該腳本允許編輯問題和答案的單獨文件。 如果不編寫看起來可笑,效率低下的代碼,我似乎無法弄清楚什么似乎行之有效。
我希望它能夠向文件添加全新的問題/答案,以及編輯已經存在的問題/答案。 關於如何進行這項工作的任何建議?
編輯:說我有一個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>
因為缺乏基礎知識,所以您產生了不好的代碼,因此, 有一兩本關於初學者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
運行程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.