2013-04-07 116 views
1

我是新來的Perl,我試圖包括一個CSS文件在perl生成的HTML頁面, 我怎麼能用cgi來做到這一點? 它應該是類似於 print $ cgi - > start_html(-link => {-href =>'/styles/main.css',-rel =>'stylsheet',-type =>'text/css'} );添加鏈接到perl CGI的頭

下面是完整的代碼,

#!C:\Dwimperl\perl\bin\perl.exe 
use CGI; 
my $cgi = new CGI; 

# ###----- GETTING THE SCRIPTS AND STYLES ------### 
my $jquery = "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"; 

# ###----- PRINTING THE PAGE -----### 
print $cgi -> header; 
print $cgi -> start_html(
      -title => "TestTitle", 
      -link => { -href => '/styles/main.css', -rel => 'stylsheet', -type => 'text/css'}, 
      -script=> [{-language => 'javascript',-src => $jquery},{-code => $jscript}], 
     ); 
print $cgi -> h1({-class=>"title",-align=>"center"},"Zero Testing"); 
print $cgi -> start_div({-align=>"center"}); 
print $cgi -> start_div({-align=>"center",-style=>"margin-bottom:35px;"}); 
print $cgi -> a({-class=>"linkButton",-id=>"btn_Page",-href=>"Test.pl"},"Suites Page"); 
print $cgi -> end_div; 
print $cgi -> start_form({-method=>"get",-id=>"dataForm"}); 
print $cgi -> end_form; 
print $cgi -> end_div; 
print $cgi -> end_html(); 

感謝

+0

你真的不希望使用CGI.pm來生成你的HTML。將HTML放入模板是一個更好的主意。 – 2013-04-07 17:11:50

回答

1

你可以這樣寫:

use CGI qw/:standard/; 

# ...  

print $cgi->start_html(
    -title =>"TestTitle", 
    -script=> [ {-language => 'javascript',-src => $jquery}, {-code => $jscript} ], 
    -head => [ 
     Link({ -href => '/styles/main.css', -rel => 'stylsheet', -type => 'text/css'}), 
    ] 
); 
+0

感謝您的回答,我按照您提到的那樣做了,但是我得到的只是一個沒有內部元素的白色屏幕。 – Zero 2013-04-07 09:33:57

+0

您是否正確初始化了$ jscript變量? – 2013-04-07 10:51:28

+0

是的,我沒有,但我沒有包括它來減小代碼大小。 – Zero 2013-04-09 19:05:46