my $page = $args{'p'};
exit 1 if $page =~ /[^\d\w]/;
print "Content-Type: text/html\n\n";
print "<html><head></head><body>";
require "$page.pl";
somefunc();
print "</body></html>";
這有什麼錯用輸出已經開始或都應該是需要在腳本的頂部之後,需要?
my $page = $args{'p'};
exit 1 if $page =~ /[^\d\w]/;
print "Content-Type: text/html\n\n";
print "<html><head></head><body>";
require "$page.pl";
somefunc();
print "</body></html>";
這有什麼錯用輸出已經開始或都應該是需要在腳本的頂部之後,需要?
我不認爲這有什麼問題。
但是,如果您希望或需要更多的腳本一致性,您可以將所需腳本中的代碼重寫爲子例程。例如:
##### old page.pl ######
print "This is the body.<P>\n";
1;
##### old cgi script #####
print "Content-type: text/html\n\n";
print "<html><head></head><body>\n";
require "page.pl";
##### new page.pl ######
sub page_body {
print "This is the body.<P>\n";
}
1;
##### new cgi script #####
require "page.pl"; # now at the top of script
print "Content-type: text/html\n\n";
print "<html><head></head><body>\n";
&page_body;
事實上,重構舊的整體cgi腳本(包括內聯HTML,沒有函數等)的第一步是將功能劃分爲單獨的文件和使用'require'。 – Ether 2010-02-01 18:19:37
不,沒有必要將所有require
都置頂。雖然,如果require
失敗,您的HTML將被中途發送。 :-P
沒有錯吧,比什麼是錯的,通常用'要求 「文件」 等;'重用代碼。 – daotoad 2010-02-01 21:55:12