麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Perl > 正文

perl 中 CGI 模塊的使用簡介

2019-11-06 08:22:34
字體:
來源:轉載
供稿:網友

一 CGI.pm中的方法(routines)調用

1. CGI.pm實現了兩種使用方法,分別是面向對象的方式和傳統的perlmodule方法的方式。面向對象的方式:

復制代碼代碼如下:#!/usr/local/bin/perl -wuse CGI;   # load CGI routines$q = CGI->new;                        # create new CGI objectPRint $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/perluse CGI qw/:standard/;           # load standard CGI routinesprint header,                    # create the HTTP header     start_html('hello world'), # start the HTML     h1('hello world'),         # level 1 header     end_html;                  # end the HTML2. 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 scalarparam()獲取參數的結果可以為scalar或array類型,例如當參數的結果來自多選的scrollinglist的時候就為array類型。如果參數的值在querystring中沒有給定("name1=&name2="),param()將返回emptystring。如果參數在querystring中根本不存在,則param()則返回undef或emptylist。當參數為多個值時querystring中寫法為var1=value1&var1=value2&var1=value3.三 header and start_html1. header指定html的header,例如復制代碼代碼如下:print header;   # 返回默認的type:text/htmlprint 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.jb51.net/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。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 福利免费观看 | 九九看片| 亚洲午夜影院在线观看 | 日韩毛片在线看 | 国产免费人做人爱午夜视频 | 欧美成人激情 | 美女喷水网站 | 美女久久| 91九色电影 | 毛片在线免费播放 | 久久91亚洲人成电影网站 | 草久在线 | 7777网站| 欧美xxxx精品另类 | 无码专区aaaaaa免费视频 | 国产自91精品一区二区 | 色网免费观看 | 国产亚洲综合一区二区 | 中文字幕观看 | 国产亚洲精品网站 | 小视频免费在线观看 | 久久91久久久久麻豆精品 | 在线a毛片 | 免费视频xxxx | 青草av.久久免费一区 | 激情小说激情图片激情电影 | 最新久久免费视频 | 国产一区精品在线观看 | 最新午夜综合福利视频 | 国产午夜精品一区二区三区免费 | 蜜桃91丨九色丨蝌蚪91桃色 | 91成人影库 | 国产精品久久久久久影院8一贰佰 | 精品国产一区二区三区四区在线 | 久久久久国产成人免费精品免费 | 人成免费网站 | 国产免费成人 | 女教师~淫辱の动漫在线 | 国产九色在线观看 | 成人毛片100部 | 蜜桃网站免费 |