html网页和cgi程序编程,CGI 编程方式学习

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/;

print

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) {

print

"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/;

print

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) {

print

"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;

print

$q->header,

$q->start_html(-title=>'The test CGI'),

"hello word!"

$q->end_html;

就这么简单。

功能型编程没有面向对象编程灵活,它里面的都定义好了,面向对象的可以想要的时候自己去定义,个人喜欢用面向对象编程方式去编写CGI的脚本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值