因此。我是perl的新手。这个简单的脚本只是创建一个具有指定维度的表,显示数字,就像在时间表中一样。我正在尝试通过使用如下所示的路径信息来记录脚本刷新次数:script.cgi / 1
除非刷新,否则会执行以下操作: script.cgi(第一次使用) script.cgi / 1 script.cgi / 1 / script.cgi / 2
为什么会这样?
脚本:
#!/usr/bin/perl
use warnings;
use strict;
BEGIN {
$| = 1;
open(STDERR,">&STDOUT");
print "Content-type:text/html\n\n";
}
print "<html>\n<head>\n<title>Homework 5</title>\n</head>\n<body>\n";
print "<h1>Homework 5B</h1>\n<hr />\n";
if(defined($ENV{'CONTENT_LENGTH'}))
{
my %FORM;
my $buffer = {};
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/,$buffer);
my $name=0; my $value=0;
foreach my $pair (@pairs)
{
($name,$value) = split(/=/,$pair);
$FORM{$name} = $value;
}
if($ENV{'CONTENT_LENGTH'}>0)
{
my @table = [];
my $var1 = $FORM{'rows'};
my $var2 = $FORM{'columns'};
if(
(!($FORM{'rows'} =~ m/[^0-9]/))
&& (!($FORM{'columns'} =~ m/[^0-9]/))
&& (!($FORM{'inc'} =~ m/[^0-9]/))
&& (!($FORM{'start'} =~ m/[^0-9]/))
)
{
my $place = $FORM{'start'};
my $i=-1;
while($i++ < $var1)
{
my $j=-1;
$table[$i] = [];
while($j++ < $var2)
{
$table[$i][$j] = $place;
$place+=$FORM{'inc'};
}
}
}
print "<table border=1>\n";
my $var =0; my $before = "<td>"; my $after = "</td>";
for(my $i=0;$i<$FORM{'rows'};$i++)
{
$var = $i+1;
print "<tr><td>Row $var:</td>\n";
for(my $j=0;$j<$FORM{'columns'};$j++)
{
my $pr = $before.($table[$i][$j]).$after;
print $pr;
}
print "</tr>\n";
}
print "</table>\n";
my $refreshed = 0;
if(defined($ENV{'PATH_INFO'}))
{
my $tmp = split(/\//,$ENV{'PATH_INFO'});
$refreshed = $tmp;
if($refreshed==1)
{
print "<p>This page was refreshed by this script one time.</p>\n";
}
elsif($refreshed>1)
{
print "<p>This page was refreshed by this script $refreshed times</p>\n";
}
}
my $path = "";
my $next = $refreshed+1;
if($next>1)
{
$path = "hw5B.cgi/".$next."/";
}
else
{
$path = "hw5B.cgi/1/";
}
print "<p>$path</p>\n";
print "<form method=\"post\" action=\"$path\">\n";
print "<span>Enter row number:<input type=\"text\" name=\"rows\" size=3 value=$FORM{'rows'} /></span><br />\n";
print "<span>Enter columns number:<input type=\"text\" name=\"columns\" size=3 value=$FORM{'columns'} /></span><br />\n";
print "<span>Enter starting number:<input type=\"text\" name=\"start\" size=3 value=$FORM{'start'} /></span><br />\n";
print "<span>Enter increment:<input type=\"text\" name=\"inc\" size=3 value=$FORM{'inc'} /></span><br />\n";
}
else
{
print "<form method=\"post\" action=\"hw5B.cgi\">\n";
print "<span>Enter row number:<input type=\"text\" name=\"rows\" size=3 /></span><br />\n";
print "<span>Enter columns number:<input type=\"text\" name=\"columns\" size=3 /></span><br />\n";
print "<span>Enter starting number:<input type=\"text\" name=\"start\" size=3 /></span><br />\n";
print "<span>Enter increment:<input type=\"text\" name=\"inc\" size=3 /></span><br />\n";
}
}
else
{
print "<form method=\"post\" action=\"hw5B.cgi\">\n";
print "<span>Enter row number:<input type=\"text\" name=\"rows\" size=3 /></span><br />\n";
print "<span>Enter columns number:<input type=\"text\" name=\"columns\" size=3 /></span><br />\n";
print "<span>Enter starting number:<input type=\"text\" name=\"start\" size=3 /></span><br />\n";
print "<span>Enter increment:<input type=\"text\" name=\"inc\" size=3 /></span><br />\n";
}
print "<input type=\"submit\" value=\"submit\" />\n";
print "</form>\n</body>\n</html>\n";
答案 0 :(得分:2)
您在post方法中使用相对路径。因此,您的服务器每次都会附加到当前路径。您可以放置一个绝对路径来减轻问题,或者您可以将此计数器包含为cgi参数而不是路径内。
<input type="hidden" name="counter" value="$counter">
另外,为了让您的生活更轻松,我建议您使用另一种双引号分隔符,这样您就不必转义html中的所有双引号:
print qq{<form method="post" action="hw5B.cgi">\n};