【发布时间】:2014-06-17 06:40:11
【问题描述】:
所以我编写了一个 perl 脚本来从 HTML 文件中传递一个参数,然后获取该参数的值并将其写入文件,然后读取文件并编译数据。这是html文件的主体:
<!DOCTYPE html>
<!DOCTYPE html>
<html lang = "en">
<head>
<title> poll.html </title>
<meta charset = "utf-8" />
<style type = "text/css">
</style>
</head>
<!-- the quiz -->
<body>
<form action = "../cgi-bin/poll.pl" method = "post">
</h1> this is a poll<br><br>What is your favorite color?</h1>
<input type="radio" name="color" value="red">Red<br>
<input type="radio" name="color" value="green">Green<br>
<input type="radio" name="color" value="blue">Blue<br>
<input type = "submit" value = "Submit Quiz" />
</form>
</body>
</html>
然后我的 perl 脚本可以正常工作,但是一旦打开文件,我就会丢失 $color 的值或 poll.pl 第 19 行的字符串。)这里是 perl 脚本: #!/usr/bin/perl -w
# processOrder.pl
use CGI ":standard";
use strict;
use warnings;
print header;
print start_html("Pizza Places Order Form");
#Set local variables to the parameter values
our($color)=param("color");
my $filename = 'data.txt';
open(my $fh, '+>>', $filename) or die "Could not open file '$filename' $!";
print $fh "'$color'\n";
my $red = 0;
my $blue = 0;
my $green = 0;
while( my $line = <$fh>) {
if (index($line, "red\n") != -1) {
$blue = $blue + 1;}
if (index($line, "blue\n") != -1) {
$blue = $blue + 1;}
if (index($line, "gren\n") != -1) {
$green = $green + 1;}
}
my $total = $red + $green +$blue;
if ($total == 0){
$total = 1}
print h4("percent blue = ", $blue/$total, "\n");
print h4("percent green = ", $green/$total, "\n");
print h4("percent red = ", $red/$total, "\n");
close $fh;
最后警告我是 perl 的新手,但我确实认为这个逻辑是合理的,任何帮助都会很棒。谢谢
【问题讨论】:
-
你“失去了$color的价值”是什么意思?
-
当使用 /usr/bin/perl 编译脚本时会遇到一个错误,提示我无法将 $color 值打印到此文件,因为它尚未初始化。我初始化了它,但编译器认为它还没有初始化,我似乎无法找到一种方法让它认为它已经初始化。
标签: perl