一 CGI.pm中的方法(routines)調(diào)用
1. CGI.pm實(shí)現(xiàn)了兩種使用方法,分別是面向?qū)ο蟮姆绞胶蛡鹘y(tǒng)的perlmodule方法的方式。
面向?qū)ο蟮姆绞剑?/P>
代碼如下:
#!/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方法的方式:
代碼如下:
#!/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ù)的方式來調(diào)用,例如:
代碼如下:
print $q->header(-type=>'image/gif',-expires=>'+3d');
命名參數(shù)的值可以為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中都有對(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>
h1('some','contents'); <h1>some contents</h1>
h1({-align=>left}); <h1 align="LEFT">
新聞熱點(diǎn)
疑難解答
圖片精選