參考:
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
完!