二 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=>'[email protected]', -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', 'yes']), td(['Onions' ,'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
完!
感谢,Thanks!
总结以上是内存溢出为你收集整理的perl-cgi高级全部内容,希望文章能够帮你解决perl-cgi高级所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)