1.大家都知道CGI是通用網關接口,可以用來編寫動態網頁。而且CGI可以用很多種語言來寫,用perl來編寫最常見,
我這里就是用perl來編寫做例子。講到編寫CGI編程方式,編寫CGI有兩程編程風格。
(1)功能型編程(function-oriented style)
這種編程方式,不要自己去創建一個對象了,它本身就內置好了一個對象去使用。雖然它內置了一個對象,有些功能并沒有
都加載進去,這里面可以自己定義開起哪些功能。注:在加載功能集時前面要加一上 : 才行
如
#!/usr/bin/perl?–w
use?CGI?qw(:standard);
一. :cgi
加載cgi-handing methods,如param().
二. :form
加載form表單,如textfied().
三. :html2 :html3 :html4
加載所有html2標簽,加載所有html3標簽,加載所有html4標簽
四. :netscape
加載所有, and
五. :html
加載這個就相當于加載了'html2','html3','html4','netscape'。
六. :standard
加載這個就相當于加一個標準的CGI,就等于加載了'html2', 'html3', 'html4', 'form' 和 'cgi'。
七. :all
將加載所有可用的功能集。
例子:這個例子引用的是perldoc-CGI 上面的
#!/usr/bin/perl?-w
use?CGI?qw/:standard/;
header,
start_html('Simple?Script'),
h1('Simple?Script'),
start_form,
"What's?your?name??",textfield('name'),p,
"What's?the?combination?",
checkbox_group(-name=>'words',
-values=>['eenie','meenie','minie','moe'],
-defaults=>['eenie','moe']),p,
"What's?your?favorite?color?",
popup_menu(-name=>'color',
-values=>['red','green','blue','chartreuse']),p,
submit,
end_form,
hr,"\n";
if?(param)?{
"Your?name?is?",em(param('name')),p,
"The?keywords?are:?",em(join(",?",param('words'))),p,
"Your?favorite?color?is?",em(param('color')),".\n";
}
print?end_html;
還有一些其它的功能,現在就不講了,講一個cgi調試的功能,
-debug
#!/usr/bin/perl?-w
use?CGI?qw/:standard?-debug/;
header,
start_html('Simple?Script'),
h1('Simple?Script'),
start_form,
"What's?your?name??",textfield('name'),p,
"What's?the?combination?",
checkbox_group(-name=>'words',
-values=>['eenie','meenie','minie','moe'],
-defaults=>['eenie','moe']),p,
"What's?your?favorite?color?",
popup_menu(-name=>'color',
-values=>['red','green','blue','chartreuse']),p,
submit,
end_form,
hr,"\n";
if?(param)?{
"Your?name?is?",em(param('name')),p,
"The?keywords?are:?",em(join(",?",param('words'))),p,
"Your?favorite?color?is?",em(param('color')),".\n";
}
print?end_html;
這樣可調試,用戶輸入的任何信息。
(2)面向對象編程(object-oriented style)
這程編程方式,沒有創建默認的對象,需要自己去創建。
如
#!/usr/bin/perl?–w
use?CGI;
my?$q?=?new?CGI;
$q->header,
$q->start_html(-title=>'The?test?CGI'),
"hello?word!"
$q->end_html;
就這么簡單。
功能型編程沒有面向對象編程靈活,它里面的都定義好了,面向對象的可以想要的時候自己去定義,個人喜歡用面向對象編程方式去編寫CGI的腳本。