来源:
http://www.cnblogs.com/itech/archive/2012/10/07/2714393.html
一 CGI.pm中的方法(routines)调用
1. CGI.pm实现了两种使用方法,分别是面向对象的方式和传统的perlmodule方法的方式。
面向对象的方式:
#!/usr/local/bin/perl -w
use CGI; # load CGI routines
$q = CGI->new; # create new CGI object
print $q->header, # create the HTTP header
$q->start_html('hello world'), # start the HTML
$q->h1('hello world'), # level 1 header
$q->end_html; # end the HTML
传统的module方法的方式:
#!/usr/local/bin/perl
use CGI qw/:standard/; # load standard CGI routines
print header, # create the HTTP header
start_html('hello world'), # start the HTML
h1('hello world'), # level 1 header
end_html; # end the HTML
2. CGI.pm中的方法。
CGI.pm中的方法,通常有很多的参数,所以一般我们使用命名参数的方式来调用,例如:
print $q->header(-type=>'image/gif',-expires=>'+3d');
命名参数的值可以为scalar或array reference类型,例如:
$q->param(-name=>'veggie',-value=>'tomato');
$q->param(-name=>'veggie',-value=>['tomato','tomahto','potato','potahto']);
3. CGI.pm中的html元素(html shortcuts)方法
所有的html的元素(例如h1,br等)在CGI.pm中都有对应的方法,这些方法根据需要动态的生成,且都包含2个参数,第一个参数为hash类型,对应html元素的属性,第二个参数的string类型,对应html元素的内容。例如html中的h1对应的方法为h1( ):
Code Generated HTML
---- --------------
h1() <h1>
h1('some','contents'); <h1>some contents</h1>
h1({-align=>left}); <h1 align="LEFT">
h1({-align=>left},'contents'); <h1 align="LEFT">contents</h1>
有时你想自己处理元素的开始和结尾,则可以使用start_tag_name和end_tag_name,例如
print start_h1,'Level 1 Header',end_h1;
有的时候start和end方法没有被自动生成,需要显示的指定,例如:
use CGI qw/:standard *table start_ul/;
用来自动生成start_table,end_table,start_ul和end_ul方法。
另一个实例:
print a({-href=>'fred.html',-target=>'_new'}, "Open a new frame");
<a href="fred.html",target="_new">Open a new frame</a>
二 CGI.pm中获取cgi的参数
@names = $query->param #get all params
@values = $query->param('foo'); #get param foo as list
$value = $query->param('foo'); #get param foo as scalar
param()获取参数的结果可以为scalar或array类型,例如当参数的结果来自多选的scrollinglist的时候就为array类型。如果参数的值在querystring中没有给定("name1=&name2="),param()将返回emptystring。如果参数在querystring中根本不存在,则param()则返回undef或emptylist。当参数为多个值时querystring中写法为var1=value1&var1=value2&var1=value3.
三 header and start_html
1. header指定html的header,例如
print header; # 返回默认的type:text/html
print header('image/gif'); #设定type为:image/gif
print header('text/html','204 No response');
$cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx's Question");
$cookie2 = $q->cookie(-name=>'answers', -value=>\%answers);
print header(-type=>'image/gif',
-nph=>1,
-status=>'402 Payment required',
-expires=>'+3d',
-cookie => [$cookie1,$cookie2] ,
-charset=>'utf-7',
-attachment=>'foo.gif',
-Cost=>'$2.00');
其中-type,-status,-expires,-cookie为可以设别的参数,其他的命名参数都被转化为html header属性。
-expires的值可以为:
+30s 30 seconds from now
+10m ten minutes from now
+1h one hour from now
-1d yesterday (i.e. "ASAP!")
now immediately
+3M in three months
+10y in ten years time
Thursday, 25-Apr-1999 00:40:33 GMT at the indicated time & date
2. start_html 创建页面的顶层元素<html><header</header><body>
例如:
print start_html(-title=>'Secrets of the Pyramids',
-author=>'fred@capricorn.org',
-base=>'true',
-target=>'_blank',
-meta=>{'keywords'=>'pharaoh secret mummy',
'copyright'=>'copyright 1996 King Tut'},
-style=>{'src'=>'/styles/style1.css'},
-BGCOLOR=>'blue');
或者:
print start_html(-head=>[
Link({-rel=>'shortcut icon',href=>'favicon.ico'}),
meta({-http_equiv => 'Content-Type',-content=> 'text/html'})
]
);
在header中加入javascript的例子:
$query = CGI->new;
print header;
$JSCRIPT=<<END;
// Ask a silly question
function riddle_me_this() {
var r = prompt("What walks on four legs in the morning, " +
"two legs in the afternoon, " +
"and three legs in the evening?");
response(r);
}
// Get a silly answer
function response(answer) {
if (answer == "man")
alert("Right you are!");
else
alert("Wrong! Guess again.");
}
END
print start_html(-title=>'The Riddle of the Sphinx',
-script=>$JSCRIPT);
print $q->start_html(-title=>'The Riddle of the Sphinx',
-script=>{-type=>'JAVASCRIPT',
-src=>'/javascript/sphinx.js'}
);
print $q->start_html(-title=>'The Riddle of the Sphinx',
-script=>[
{ -type => 'text/javascript',
-src => '/javascript/utilities10.js'
},
{ -type => 'text/javascript',
-src => '/javascript/utilities11.js'
},
{ -type => 'text/jscript',
-src => '/javascript/utilities12.js'
},
{ -type => 'text/ecmascript',
-src => '/javascript/utilities219.js'
}
]
);
在header中使用css的例子:
use CGI qw/:standard :html3/;
#here's a stylesheet incorporated directly into the page
$newStyle=<<END;
<!--
P.Tip {
margin-right: 50pt;
margin-left: 50pt;
color: red;
}
P.Alert {
font-size: 30pt;
font-family: sans-serif;
color: red;
}
-->
END
print header();
print start_html( -title=>'CGI with Style',
-style=>{-src=>'http://www.capricorn.com/style/st1.css',
-code=>$newStyle}
);
print h1('CGI with Style'),
p({-class=>'Tip'},
"Better read the cascading style sheet spec before playing with this!"),
span({-style=>'color: magenta'},
"Look Mom, no hands!",
p(),
"Whooo wee!"
);
print end_html;
四 url
$full_url = url(); # http://your.host.com/path/to/script.cgi
$full_url = url(-full=>1); # http://your.host.com/path/to/script.cgi
$relative_url = url(-relative=>1); #script.cgi
$absolute_url = url(-absolute=>1); #path/to/script.cgi
$url_with_path = url(-path_info=>1);
$url_with_path_and_query = url(-path_info=>1,-query=>1);
$netloc = url(-base => 1); #http://your.host.com
五 CGI.pm中的html元素方法的特殊用法
如果元素的第二个参数为list类型,则会被分解,例如:
print ul(
li({-type=>'disc'},['Sneezy','Doc','Sleepy','Happy'])
);
相当于:
<ul>
<li type="disc">Sneezy</li>
<li type="disc">Doc</li>
<li type="disc">Sleepy</li>
<li type="disc">Happy</li>
</ul>
例如table可以写为:
print table({-border=>undef},
caption('When Should You Eat Your Vegetables?'),
Tr({-align=>'CENTER',-valign=>'TOP'},
[
th(['Vegetable', 'Breakfast','Lunch','Dinner']),
td(['Tomatoes' , 'no', 'yes', 'yes']),
td(['Broccoli' , 'no', 'no', 'yes']),
td(['Onions' , 'yes','yes', 'yes'])
]
)
);
六 CGI.pm中非标准的html元素方法
print comment('here is my comment'); #generates an HTML comment (<!-- comment -->)
因为与perl方法冲突,所以大写的:
Select
Tr
Link
Delete
Accept
Sub
其他特殊的html元素方法:start_html(), end_html(), start_form(), end_form(), start_multipart_form() and all the fill-out form tags。
七 CGI.pm中的form相关
1 start_form 和start_multipart_form
print start_form(-method=>$method,
-action=>$action,
-enctype=>$encoding);
<... various form stuff ...>
print end_form;
-or-
print start_form($method,$action,$encoding);
<... various form stuff ...>
print end_form;
如果没有指定method,action,enctype,默认地为:
method: POST
action: this script
enctype: application/x-www-form-urlencoded for non-XHTML
multipart/form-data for XHTML, see multipart/form-data below.
当使用start_form的时候,enctype为application/x-www-form-urlencoded,如果需要新式的xhtml,则需要使用start_multipart_form,此时enctype为multipart/form-data。
更多参考cgi man page: http://search.cpan.org/~markstos/CGI.pm-3.60/lib/CGI.pm
- Perl CGI编程
http://www.runoob.com/perl/perl-cgi-programming.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Ga ...
- 《Apache服务之php/perl/cgi语言的支持》RHEL6——服务的优先级
安装php软件包: 安装文本浏览器 安装apache的帮助文档: 测试下是否ok 启动Apache服务关闭火墙: 编辑一个php测试页测试下: perl语言包默认系统已经安装了,直接测试下: Apac ...
- Perl 语法 - 高级特性
总结: q().qq().qw(同单引号).qx{牢记是花括号},分别是单引号.双引号.创建字符串列表 和 捕获命令输出. 第9学时 其他函数和运算符 一件事情可以使用多种方法完成. 有哪些其他的 ...
- perl数组高级
1 去除一个数组中的重复元素: 使用grep函数代码片段: 代码: my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 ); my %count; ...
- Perl & Python编写CGI
近期偶然玩了一下CGI,收集点资料写篇在这里留档. 如今想做HTTP Cache回归測试了,为了模拟不同的响应头及数据大小.就须要一个CGI按须要传回指定的响应头和内容.这是从老外的測试页面学习到的经 ...
- 关于学习Perl
Perl是一门很有用的语言,可以用它来做很多事.然而,它也仅是一门语言,掌握了Perl,你只是掌握了Computer领域的一小块知识.在学习Perl前,请明确你的学习目的,并采用正确的学习方法和资源. ...
- Perl的调试方法
来源: http://my.oschina.net/alphajay/blog/52172 http://www.cnblogs.com/baiyanhuang/archive/2009/11/09/ ...
- Perl 认识简介
Perl简介 Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl 是高级.通用.直译 ...
- Perl语言
Perl是高级.通用.直译式.动态的程序语言家族.最初设计者拉里·沃尔(Larry Wall)为了让在UNIX上进行报表处理的工作变得更方便,决定开发一个通用的脚本语言,而在1987年12月18日发表 ...
- Perl的IO操作(2):更多文件句柄模式
open函数除了> >> <这三种最基本的文件句柄模式,还支持更丰富的操作模式,例如管道.其实bash shell支持的重定向模式,perl都支持,即使是2>&1 ...
随机推荐
- unity3d 学习过程记录
通过泰克在线,开始学习视频教程 1.学习 Unity3d零基础教学初级篇:熟悉一下unity编辑器结构和控件以及对控件的基本操作 2.学习Roll-a-ball小项目开发:通过练习制作小游戏,巩固所学 ...
- javaWEB总结(15):jsp指令_page指令
jsp指令 jsp指令是为jsp引擎设计的.他们并不直接产生任何可见输出,而只是告诉引擎如何处理jsp页面的其余部分. 目前定义了page,include和taglib这三种指令. page指令 (1 ...
- 【第七篇】Volley之处理Gzip数据
一般对于API请求需带上GZip压缩,因为API返回数据大都是Json串之类字符串,GZip压缩后内容大小大幅降低. public class GZipRequest extends StringRe ...
- linux下如何使用vnstat查看服务器带宽流量统计
因为很多vps或者服务器都是限流量的,但是又很多服务商并没有提供详细的流量表,比如每天的流量表,所以肯定有人很想知道自己服务器到底跑了多少流量. vnstat就是一个很好用的服务器流量统计命令.我截几 ...
- HDU2037今年暑假不AC(贪心)
Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACM ...
- GitHub版本控制工具入门(一)
你还在使用 SVN 吗? 那就太 low 了... 概述 要了解 github 要从另外一个软件开始,那就是 git ,他的中文意思是 傻子 ,这款软件为什么要起一个这么傻的名字呢? 那你就需要了解一 ...
- 一个简单的Servlet工具
以前老师在项目中用过一个Sevlet的工具,就是在请求Servlet的时候带一个参数,该参数决定要执行Servlet中的方法, public class ServletUtils extends Ht ...
- [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路
14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...
- Quicksum
Quicksum Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Subm ...
- zookeeper的安装及集群配置
1.解压 2.修改配置文件 cp zoo_sample.cfg zoo.cfg vim zoo.cfg dataDir=/usr/local/zookeeperData 其余采用默认 参数说明: ti ...