我想通过接受ip1到ip10值来创建CSV文件,这些值是通过POST方法从HTML文件发送的。 test.cgi文件包含perl代码。我试着编写如下代码。请帮我。
#!/usr/bin/perl -w
use CGI qw(:standard);
use strict;
use warnings;
$ip1 = $query->param("ip1");
$ip2 = $query->param("ip2");
$ip3 = $query->param("ip3");
$ip4 = $query->param("ip4");
$ip5 = $query->param("ip5");
$ip6 = $query->param("ip6");
$ip7 = $query->param("ip7");
$ip8 = $query->param("ip8");
$ip9 = $query->param("ip9");
$ip10 = $query->param("ip10");
$ie1 = $query->param("ie1");
$ie2 = $query->param("ie2");
$ie3 = $query->param("ie3");
$ie4 = $query->param("ie4");
$ie5 = $query->param("ie5");
$ie6 = $query->param("ie6");
$ie7 = $query->param("ie7");
$ie8 = $query->param("ie8");
$ie9 = $query->param("ie9");
$ie10 = $query->param("ie10");
open(my $fh, ">", "test.csv")or die "$!";
print $fh ",zone A,zone B,zone C,zone D,zone E,zone F,zone G,zone H,zone I,zone j\n";
print $fh "IP,$ip1,$ip2,$ip3,$ip4,$ip5,$ip6,$ip7,$ip8,$ip9,$ip10\n";
print $fh "IE,$ie1,$ie2,$ie3,$ie4,$ie5,$ie6,$ie7,$ie8,$ie9,$ie10\n";
close $fh;`
如果通过post方法假设ip的接收值为1到10,而ie为11到20,那么我在CSV文件中的预期输出如下所示:
,zone A,zone B,zone C,zone D,zone E,zone F,zone G,zone H,zone I,zone j
IP,1,2,3,4,5,6,7,8,9,10
IE,11,12,13,14,15,16,17,18,19,20
答案 0 :(得分:0)
您是否查看了Web服务器错误日志?在我看来,这个程序甚至不会编译。它充满了未声明的变量。为了使它甚至编译,你需要将它改为这样的东西:
#!/usr/bin/perl -w
use CGI qw(:standard);
use strict;
use warnings;
my $query = CGI->new;
my $ip1 = $query->param("ip1");
my $ip2 = $query->param("ip2");
my $ip3 = $query->param("ip3");
my $ip4 = $query->param("ip4");
my $ip5 = $query->param("ip5");
my $ip6 = $query->param("ip6");
my $ip7 = $query->param("ip7");
my $ip8 = $query->param("ip8");
my $ip9 = $query->param("ip9");
my $ip10 = $query->param("ip10");
my $ie1 = $query->param("ie1");
my $ie2 = $query->param("ie2");
my $ie3 = $query->param("ie3");
my $ie4 = $query->param("ie4");
my $ie5 = $query->param("ie5");
my $ie6 = $query->param("ie6");
my $ie7 = $query->param("ie7");
my $ie8 = $query->param("ie8");
my $ie9 = $query->param("ie9");
my $ie10 = $query->param("ie10");
open(my $fh, ">", "test.csv")or die "$!";
print $fh ",zone A,zone B,zone C,zone D,zone E,zone F,zone G,zone H,zone I,zone j\n";
print $fh "IP,$ip1,$ip2,$ip3,$ip4,$ip5,$ip6,$ip7,$ip8,$ip9,$ip10\n";
print $fh "IE,$ie1,$ie2,$ie3,$ie4,$ie5,$ie6,$ie7,$ie8,$ie9,$ie10\n";
close $fh;
但请不要这样做。这非常浪费变量。难道你不知道全球变量短缺吗? : - )
当您看到像您这样的重复代码时,您应该考虑使用数组或散列来简化它。我会写这样的代码。
#!/usr/bin/perl
use CGI qw(param header);
use strict;
use warnings;
my (@ip, @ie);
for (1 .. 10) {
push @ip, param("ip$_");
push @ie, param("ie$_");
}
# You'll need to ensure that test.csv is somewhere that your CGI program
# has permission to write files.
open(my $fh, '>', 'test.csv')or die $!;
print $fh ',', join(',', map { "zone $_" } 'A' .. 'J'), "\n";
print $fh 'IP,', join(',', @ip), "\n";
print $fh 'IE,', join(',', @ie), "\n";
close $fh;
# You'll probably want to do something more interesting here too.
print header('text/plain');
print 'File created';