一 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">
新聞熱點
疑難解答