记一次学习perl写CGI遇到的ERROR

初学者在使用Perl编写CGI脚本时遇到错误,问题在于缺少cgi.pm模块。通过手动或自动安装CGI模块,然后改用CGI::Fast解决编译错误,最终成功运行并显示正常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学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访问正常!

问题解决!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值