我正在创建一个网站,它将获取用户信息并存储在mysql数据库中。当我通过终端运行脚本时,我收到以下错误:
Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at ./folder1/friends-mysql.cgi line 16.
我不确定为什么会收到此错误。这是我的剧本:
#!/usr/bin/perl -w
use DBI;
#Declare Variables with MYSQL Connection Data
$db="my database";
$user="my username";
$passwd="password";
$host="host";
$connectionInfo="";
#Print HTTP header
print "Content-type:text/html\n\n";
#Check if Method is GET
if ($ENV{REQUEST_METHOD} eq "GET") { -->This is line 16
&displayform();
exit;
}
else { #If Method not GET process form and insert into DB
&parseform();
&insertfriend();
exit;
}
#Standard form parsing using POST method
sub parseform {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair(@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
答案 0 :(得分:1)
当您的程序作为Web应用程序运行时,它是CGI协议设置多个环境变量,包括REQUEST_METHOD
如果要从命令行测试程序,则必须在运行程序之前手动设置必需的变量
像这样的东西
$ export REQUEST_METHOD="GET"
$ ./folder1/friends-mysql.cgi
请注意,将use strict
添加到您编写的每个Perl程序的顶部也是必需,并且use warnings 'all'
优于shebang的-w
线