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
加载所有<blink>, <fontsize> and <center> 标签。
五. :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的脚本。