Perl 是早期Web开发中广泛使用的语言,通过CGI与Web服务器交互。本文将介绍Perl CGI的基本概念、实现方法及常见应用。


目录

  1. CGI 编程概述
  2. 环境配置
  3. 基本 CGI 脚本
  4. 处理表单数据
  5. 使用 CGI 模块
  6. 安全性与调试
  7. 参考资料

1. CGI 编程概述

CGI 是Web服务器与外部程序通信的协议,Perl 通过生成动态HTML内容实现Web应用。特点:

  • 简单:适合小型Web应用。
  • 跨平台:依赖Web服务器支持。
  • 局限:性能较低,现代Web开发多用框架。

站内链接:了解文件操作,见 Perl 文件操作


2. 环境配置

Web 服务器

  • Apache:常见选择。
  • 配置CGI目录(如 /var/www/cgi-bin):
  <Directory "/var/www/cgi-bin">
      AllowOverride None
      Options +ExecCGI
      AddHandler cgi-script .cgi .pl
      Require all granted
  </Directory>

Perl 环境

确保Perl已安装,脚本需可执行权限:

chmod +x script.pl

3. 基本 CGI 脚本

简单输出

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";  # HTTP头
print "<html><body><h1>Hello, Perl CGI!</h1></body></html>";
  • 保存为 hello.pl,放在CGI目录。
  • 访问:http://localhost/cgi-bin/hello.pl

添加动态内容

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
my $time = localtime;
print <<HTML;
<html>
<body>
<h1>当前时间</h1>
<p>$time</p>
</body>
</html>
HTML

4. 处理表单数据

GET 方法

从环境变量 QUERY_STRING 获取:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
my $query = $ENV{QUERY_STRING};  # 如 name=Alice&age=25
my %params;
for my $pair (split /&/, $query) {
    my ($key, $value) = split /=/, $pair;
    $params{$key} = $value;
}

print "<html><body>";
print "<h1>欢迎, $params{name}!</h1>";
print "<p>年龄: $params{age}</p>";
print "</body></html>";

访问:http://localhost/cgi-bin/script.pl?name=Alice&age=25

POST 方法

从标准输入读取:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
my $length = $ENV{CONTENT_LENGTH};
my $data;
read(STDIN, $data, $length) if $length;
my %params = map { split /=/ } split /&/, $data;

print "<html><body>";
print "<h1>收到数据</h1>";
print "<p>姓名: $params{name}</p>";
print "</body></html>";

对应HTML表单:

<form method="POST" action="/cgi-bin/script.pl">
    姓名: <input type="text" name="name">
    <input type="submit" value="提交">
</form>

5. 使用 CGI 模块

CGI.pm 是Perl内置模块,简化参数处理和HTML生成。

安装

通常预装,若缺失:

cpan CGI

示例

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $cgi = CGI->new;
print $cgi->header;  # 输出HTTP头
print $cgi->start_html(-title => 'CGI 示例');
print $cgi->h1("欢迎, " . $cgi->param('name'));
print $cgi->end_html;

访问:http://localhost/cgi-bin/script.pl?name=Bob

表单处理

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $cgi = CGI->new;
print $cgi->header;
print $cgi->start_html(-title => '表单结果');

if ($cgi->param()) {
    my $name = $cgi->param('name');
    print $cgi->p("你输入的姓名是: $name");
} else {
    print $cgi->start_form(-method => 'POST', -action => '/cgi-bin/script.pl');
    print $cgi->p("姓名: ", $cgi->textfield('name'));
    print $cgi->submit(-value => '提交');
    print $cgi->end_form;
}
print $cgi->end_html;

6. 安全性与调试

安全性

  • 输入验证:防止注入。
my $name = $cgi->param('name');
die "无效输入" unless $name =~ /^[a-zA-Z]+$/;
  • Taint模式:检查外部输入。
#!/usr/bin/perl -T
use strict;
use CGI;

调试

  • 查看错误日志:如 /var/log/apache2/error.log
  • 输出调试信息:
print "Content-type: text/plain\n\n";
print "QUERY_STRING: $ENV{QUERY_STRING}\n";

7. 参考资料

站内链接

出站链接

其他资源

  • 《CGI Programming with Perl》 – CGI编程书籍。
  • X社区:搜索 #PerlCGI 获取示例。

这篇指南详细介绍了Perl CGI编程的基础和实用方法,从简单脚本到模块化开发一应俱全。如果需要更深入的内容(比如文件上传或现代替代方案),请告诉我!