开发者

Is there anything wrong with using requires after output starts printing?

Example:

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>";

Is there anything wrong with using the require after开发者_如何学Python the output has started or should all requires be at the top of the script?


I don't think there's anything wrong with that.

But if you want or need more consistency in your scripts, you could rewrite the code in the required script as a subroutine. For example:

##### 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;


No, there's no need for all requires to be at the top. Though, if the require fails, your HTML would be halfway sent already. :-P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜