初学perl写CGI,遇到N多问题,编写的脚本如下
[root@test02 learn_test]# vi hello.cgi
#!/usr/bin/perl
use cgi;
use strict;
use warnings;
print header;
print "<B> Hello,World!</B>";
perl -c hello.cgi
Can't locate cgi.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/apache/htdocs/learn_test/hello.cgi line 2.
apache 日志下输出:
Can't locate cgi.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/apache/htdocs/learn_test/hello.cgi line 2.
[Tue Dec 03 12:57:33 2013] [error] [client 192.168.1.12] BEGIN failed--compilation aborted at /usr/local/apache/htdocs/learn_test/hello.cgi line 2.
[Tue Dec 03 12:57:33 2013] [error] [client 192.168.1.12] Premature end of script headers: hello.cgi
解决办法:
从两个log message的输出信息来看是缺少了cgi.pm模块,可以手工或者自动安装cgi模块
自动安装:perl -e shell -MCPAN 之后install cgi
手工安装:http://www.cpan.org/modules/01modules.index.html 查找CGI的模块包CGI.pm-3.64.tar.gz
解压:tar zxvf CGI.pm-3.64.tar.gz
安装:cd CGI.pm-3.64
perl Makefile.PL
make
make install
这样就安装好了CGI的模块,但是使用perl -c检查时发现问题依旧
在/usr/local/perl5下,发现模块名为:FCGI.PM,并不是想象中的CGI.PM。查看perl的官方CGI模块介绍
http://perldoc.perl.org/CGI/Fast.html
使用模块为: use CGI::Fast qw(:standard);
于是更改脚本语句:
#!/usr/bin/perl
use CGI::Fast qw(:standard);
use strict;
use warnings;
print "Content-type:text/html\n\n";
#print header
print "<B> Hello,World!</B>";
再使用perl -c hello.cgi ,语法正确
再通过web访问正常!
问题解决!