a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 編程 > Perl > 正文

perl的cgi編程詳細(xì)解析

2020-02-23 19:47:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  今天小編跟大家分享一篇perl的cgi編程詳細(xì)解析,感興趣的朋友跟小編一起來(lái)了解一下吧!

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

  1. CGI.pm實(shí)現(xiàn)了兩種使用方法,分別是面向?qū)ο蟮姆绞胶蛡鹘y(tǒng)的perlmodule方法的方式。

  面向?qū)ο蟮姆绞剑?/p>

  復(fù)制代碼 代碼如下:

  #!/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

  傳統(tǒng)的module方法的方式:

  復(fù)制代碼 代碼如下:

  #!/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中的方法,通常有很多的參數(shù),所以一般我們使用命名參數(shù)的方式來(lái)調(diào)用,例如:

  復(fù)制代碼 代碼如下:

  print $q->header(-type=>'image/gif',-expires=>'+3d');

  命名參數(shù)的值可以為scalar或array reference類型,例如:

  復(fù)制代碼 代碼如下:

  $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中都有對(duì)應(yīng)的方法,這些方法根據(jù)需要?jiǎng)討B(tài)的生成,且都包含2個(gè)參數(shù),第一個(gè)參數(shù)為hash類型,對(duì)應(yīng)html元素的屬性,第二個(gè)參數(shù)的string類型,對(duì)應(yīng)html元素的內(nèi)容。例如html中的h1對(duì)應(yīng)的方法為h1( ):

  Code Generated HTML

  ---- --------------

  h1()

?

?

  h1('some','contents');

some contents

?

  h1({-align=>left});

?

?

  h1({-align=>left},'contents');

contents

?

  有時(shí)你想自己處理元素的開(kāi)始和結(jié)尾,則可以使用start_tag_name和end_tag_name,例如

  print start_h1,'Level 1 Header',end_h1;

  有的時(shí)候start和end方法沒(méi)有被自動(dòng)生成,需要顯示的指定,例如:

  use CGI qw/:standard *table start_ul/;

  用來(lái)自動(dòng)生成start_table,end_table,start_ul和end_ul方法。

  另一個(gè)實(shí)例:

  復(fù)制代碼 代碼如下:

  print a({-href=>'fred.html',-target=>'_new'}, "Open a new frame");

  Open a new frame

  二 CGI.pm中獲取cgi的參數(shù)

  @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()獲取參數(shù)的結(jié)果可以為scalar或array類型,例如當(dāng)參數(shù)的結(jié)果來(lái)自多選的scrollinglist的時(shí)候就為array類型。如果參數(shù)的值在querystring中沒(méi)有給定("name1=&name2="),param()將返回emptystring。如果參數(shù)在querystring中根本不存在,則param()則返回undef或emptylist。當(dāng)參數(shù)為多個(gè)值時(shí)querystring中寫法為var1=value1&var1=value2&var1=value3.

  三 header and start_html

  1. header指定html的header,例如

  復(fù)制代碼 代碼如下:

  print header; # 返回默認(rèn)的type:text/html

  print header('image/gif'); #設(shè)定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為可以設(shè)別的參數(shù),其他的命名參數(shù)都被轉(zhuǎn)化為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 創(chuàng)建頁(yè)面的頂層元素

?

?

  例如:

  復(fù)制代碼 代碼如下:

  print start_html(-title=>'Secrets of the Pyramids',

  -author=>'fred@jbxue.org',

  -base=>'true',

  -target=>'_blank',

  -meta=>{'keywords'=>'pharaoh secret mummy',

  'copyright'=>'copyright 1996 King Tut'},

  -style=>{'src'=>'/styles/style1.css'},

  -BGCOLOR=>'blue');

  或者:

  復(fù)制代碼 代碼如下:

  print start_html(-head=>[

  Link({-rel=>'shortcut icon',href=>'favicon.ico'}),

  meta({-http_equiv => 'Content-Type',-content=> 'text/html'})

  ]

  );

  在header中加入javascript的例子:

  復(fù)制代碼 代碼如下:

  $query = CGI->new;

  print header;

  $JSCRIPT= ;

  // 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的例子:

  復(fù)制代碼 代碼如下:

  use CGI qw/:standard :html3/;

  #here's a stylesheet incorporated directly into the page

  $newStyle= ;

  

  END

  print header();

  print start_html( -title=>'CGI with Style',

  -style=>{-src=>'http://www.5lwq4hdr.cn/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

  復(fù)制代碼 代碼如下:

  $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元素方法的特殊用法

  如果元素的第二個(gè)參數(shù)為list類型,則會(huì)被分解,例如:

  復(fù)制代碼 代碼如下:

  print ul(

  li({-type=>'disc'},['Sneezy','Doc','Sleepy','Happy'])

  );

  相當(dāng)于:

  

?

  

?

  

?

  

?

  

?

  

  • Sneezy
  • Doc
  • Sleepy
  • Happy

?

  例如table可以寫為:

  復(fù)制代碼 代碼如下:

  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中非標(biāo)準(zhǔn)的html元素方法

  print comment('here is my comment'); #generates an HTML comment ()

  因?yàn)榕cperl方法沖突,所以大寫的:

  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相關(guān)

  1 start_form 和start_multipart_form

  復(fù)制代碼 代碼如下:

  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;

  如果沒(méi)有指定method,action,enctype,默認(rèn)地為:

  method: POST

  action: this script

  enctype: application/x-www-form-urlencoded for non-XHTML

  multipart/form-data for XHTML, see multipart/form-data below.

  當(dāng)使用start_form的時(shí)候,enctype為application/x-www-form-urlencoded,如果需要新式的xhtml,則需要使用start_multipart_form,此時(shí)enctype為multipart/form-data。

  以上就是perl的cgi編程詳細(xì)解析,想必都了解了吧,更多相關(guān)內(nèi)容請(qǐng)繼續(xù)關(guān)注武林技術(shù)頻道。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 国产黄色大片免费观看 | 亚洲欧美另类图片 | 岛国视频在线 | 成人在线影视 | 一级片在线观看 | 激情五月综合 | a级在线| 日本免费在线观看 | 一区二区成人在线 | 久久国产亚洲精品 | 国产激情在线观看 | 日韩精品专区 | 久久综合狠狠综合久久综合88 | 欧日韩在线 | 在线电影一区 | 国产视频一区二区 | 日本精品999 | 操人网| 国产 高清 在线 | 99re国产精品视频 | 色视频网站在线观看一=区 www..99re | 久久青青视频 | 亚洲小视频 | 亚洲一区二区三区四区五区中文 | 日韩高清一区二区 | 天天爱爱网 | 羞羞视频网站在线观看 | 日韩精品资源 | 午夜视频网 | 久久精品国产久精国产 | 国产aaa大片 | a国产在线观看 | 日韩性猛交 | 成人一区二区三区四区 | 久久久噜噜噜www成人网 | 国产一级一级毛片女人精品 | 欧美成人一级片 | 国产在线啪 | 日本三级在线观看网站 | 欧美久久综合 | 国产免费久久 |