perl-cgi命令行调试


参考:
 http://docstore.mik.ua/orelly/linux/cgi/ch15_03.htm 

 http://stackoverflow.com/questions/2224158/how-can-i-send-post-and-get-data-to-a-perl-cgi-script-via-the-command-line
 http://search.cpan.org/~lds/CGI.pm-3.20/CGI.pm#DEBUGGING 

 

一 一般地我们可以使用以下方法来检查cgi脚本的错误:
1)使用-cwT来检查cgi脚本的语法,警告。例如perl -wcT your.cgi.
2)在命令行执行cgi:./calendar.cgi month=jan year=2001.

3)在命令行执行时可以交互式offline地输入cgi需要的参数, 此时cgi脚本中需要加入-debug参数 use CGI qw(-debug);,然后执行./calendar 且输入 month=jan year=2001,最后退出输入执行(use Ctrl-D on Unix or Mac; use Ctrl-Z on Windows) 。
4)将cgi放到webserver,然后通过webbrowser来对其测试,此时可以使用print来打印变量的值到html来帮助调试。也可以使用use CGI::Carp qw(warningsToBrowser fatalsToBrowser);将警告和错误打印到html。
5)检查webserver的log:tail -f /usr/local/apache/logs/error_log.

 

 

二 命令行执行cgi脚本的实例

1)

通过post方式来调用cgi脚本:

$ echo -n 'a=b;c=d' | REQUEST_METHOD=POST CONTENT_LENGTH=999 perl index.cgi

通过get方式来调用cgi脚本:

 

$ perl index.cgi 'a=b;c=d'

 

 

2)

For example, with the following program (notice -debug in the arguments to use CGI)

#! /usr/bin/perl

use warnings;
use strict;

use CGI qw/ :standard -debug /;

print "Content-type: text/plain\n\n",
      map { $_ . " => " . param($_) . "\n" }
      param;

you feed it parameters on the command line:

$ ./prog.cgi foo=bar baz=quux 
Content-type: text/plain  foo => bar baz => quux

You can also do so via the standard input:

$ ./prog.cgi (offline mode: enter name=value pairs on standard input; press ^D or ^Z when done) foo=bar baz=quux ^D
Content-type: text/plain foo => bar baz => quux
 

 

3)

当用get方式时,设置环境变量 QUERY_STRING (实例在windows上)

set QUERY_STRING=recipient=John@Doe.com&Fullname=M+Name
perl -w scriptname.cgi

 

当用post方式时,需要将query_string的内容输入到临时文件testinput.txt,例如

echo recipient=John@Doe.com&Fullname=M+Name >testinput.txt

perl -w scriptname.cgi < testinput.txt

 

 

三 来自perl cgi man page的帮助 

If you are running the script from the command line or in the perl debugger, you can pass the script a list of keywords or parameter=value pairs on the command line or from standard input (you don't have to worry about tricking your script into reading from environment variables). You can pass keywords like this:

    your_script.pl keyword1 keyword2 keyword3

or this:

   your_script.pl keyword1+keyword2+keyword3

or this:

    your_script.pl name1=value1 name2=value2

or this:

    your_script.pl name1=value1&name2=value2

To turn off this feature, use the -no_debug pragma.

To test the POST method, you may enable full debugging with the -debug pragma. This will allow you to feed newline-delimited name=value pairs to the script on standard input.

When debugging, you can use quotes and backslashes to escape characters in the familiar shell manner, letting you place spaces and other funny characters in your parameter=value pairs:

   your_script.pl "name1='I am a long value'" "name2=two\ words"

Finally, you can set the path info for the script by prefixing the first name/value parameter with the path followed by a question mark (?):

    your_script.pl /your/path/here?name1=value1&name2=value2 

 


完!

 

 

posted @   iTech  阅读(2944)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2011-09-23 Linux下集群技术应用概述
2011-09-23 整合apache和tomcat构建Web服务器
2011-09-23 浅谈Linux的内存管理机制
2011-09-23 Linux系统运行级与启动机制剖析
点击右上角即可分享
微信分享提示