语法错误,靠近“.if” (可能是从第47行开始的失控的多行“”字符串) 由于编译错误,HTMLmaker.pl的执行中止。
use strict;
use warnings;
use vars qw( $fc $sc $fontsize );
print "What would you like the title of the website to be? ";
chomp (my $TWT = <STDIN>);
print "What would you like as the background colour?
It will be a gradient, enter your top colour ";
chomp (my $firstecolour = <STDIN>);
print "Choose your second colour ";
chomp (my $secondcolour = <STDIN>);
print "How large would you like the text to be, 6 = smallest, 1 = largest ";
chomp (my $size = <STDIN>);
print "What text would you your heading to be? ";
chomp (my $text = <STDIN>);
print "What size would you like your font?";
chomp (my $fontsize = <STDIN>);
print "What font would you like?";
chomp (my $category = <STDIN>);
print "What colour would like your text to be?";
chomp (my $col = <STDIN>);
print "What would you like to put as tour first paragraph?";
chomp (my $txt = <STDIN>);
print "Would you like your text aligned in the center?";
chomp (my $ali = <STDIN>);
if ($firstecolour eq "black") { $fc = "#000000" };
if ($firstecolour eq "red") { $fc = "#FF0000" };
if ($firstecolour eq "green") { $fc = "#00FF00" };
if ($firstecolour eq "blue") { $fc = "#0000FF" };
if ($firstecolour eq "yellow") { $fc = "#FFFF00" };
if ($firstecolour eq "cyan") { $fc = "#00FFFF" };
if ($firstecolour eq "pink") { $fc = "#FF00FF" };
if ($firstecolour eq "grey") { $fc = "#C0C0C0" };
if ($firstecolour eq "white") { $fc = "#FFFFFF" };
if ($secondcolour eq "black") { $sc = "#000000" };
if ($secondcolour eq "red") { $sc = "#FF0000" };
if ($secondcolour eq "green") { $sc = "#00FF00" };
if ($secondcolour eq "blue") { $sc = "#0000FF" };
if ($secondcolour eq "yellow") { $sc = "#FFFF00" };
if ($secondcolour eq "cyan") { $sc = "#00FFFF" };
if ($secondcolour eq "pink") { $sc = "#FF00FF" };
if ($secondcolour eq "grey") { $sc = "#C0C0C0" };
if ($secondcolour eq "white") { $sc = "#FFFFFF" };
my $filename = "./index.htm";
open (LOG, ">>$filename") or die $!;
print LOG "<html>
<title>$TWT</title>
<body style=\"height: 800px; width: 1247px; filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr=\'$sc\', startColorstr=\'$fc\', gradientType=\'0\');\">
<DIV ALIGN=CENTER>
<h$size>$text</h>
</DIV>
<font size=\"$fontsize\" face=\"$category\" color=\"$col\">
". if ($ali eq "yes") { print "<DIV ALIGN=CENTER>"}; ."
<p>$txt</p></font>
". if ($ali eq "yes") { print "</DIV>"}; ."
</body>
</html>";
close (LOG);
<STDIN>;
这有什么不对吗?
答案 0 :(得分:5)
Perl中的if
语句不能是表达式的一部分,它是一个独立的构造。如果要将其放在表达式中,则必须将其包装在do {...}
块中。
内联等效词是COND ? EXPR1 : EXPR2
构造,与do {if (COND) {EXPR1} else {EXPR2}}
相同。
您应该使用用my
声明的词法变量而不是use vars
pragma。
您的代码也包含大量重复内容。 Perl非常擅长让你消除这种情况,从而节省时间,简化重构并减少错误。
例如,应使用哈希表将颜色名称映射到十六进制代码。同样,在提示用户后设置许多变量可以写成循环。
以下是我的意思的简短例子:
my @questions = (
[\my $TWT => "What would you like the title of the website to be? "],
[\my $firstecolour => "What would you like as the background colour?\nIt will be a gradient, enter your top colour: "],
[\my $secondcolour => "Choose your second colour: "],
);
for (@questions) {
print $$_[1];
chomp (${$$_[0]} = <STDIN>)
}
my %colors = do {
no warnings 'qw';
qw(
black #000000
red #FF0000
green #00FF00
blue #0000FF
yellow #FFFF00
cyan #00FFFF
pink #FF00FF
grey #C0C0C0
white #FFFFFF
)
};
for ($firstecolour, $secondcolour) {
if ($colors{lc $_}) {
$_ = $colors{lc $_}
}
}
say for $firstecolour, $secondcolour;
答案 1 :(得分:2)
您无法像过去那样内联if
声明
". if ($ali eq "yes") { print "…"}; ."…"; close (LOG); ;
==&GT;
";
if ($ali eq "yes") { print LOG "…"};
print LOG "…";
close (LOG);
答案 2 :(得分:2)
在字符串表达式中间有一条打印指令。这有两个方面是错误的:
print "<DIV ALIGN=CENTER>"
会将该文字打印到标准输出,而不是将其包含在您打印到LOG
的内容中。代码中有问题的部分是:
print LOG "<html> …
". if ($ali eq "yes") { print "<DIV ALIGN=CENTER>"}; ."
… </html>";
if
无法在表达式中使用,但您可以使用conditional operator condition ? if_true : if_false
代替。
print LOG "<html> …
" . ($ali eq "yes" ? "<DIV ALIGN=CENTER>" : "") . "
… </html>";
在此部分代码中有用的另一个Perl功能是“here-document” syntax。这是一种编写多行字符串的简单方法。您将<<EOF
占位符作为参数添加到print
,所有行直到只包含EOF
的行成为一个长字符串。放置变量表达式的@{[…]}
idiom有点难以解释;如果这让您担心,请事先将变量部分放在变量中,然后使用这些变量。
print LOG <<EOF;
<html> …
@{[$ali eq "yes" ? "<DIV ALIGN=CENTER>" : ""]}
<p>$txt</p></font>
@{[$ali eq "yes" ? "</DIV>" : ""]}
… </html>
EOF
或
my $ali_start = ($ali eq "yes" ? "<DIV ALIGN=CENTER>\n" : "");
my $ali_end = ($ali eq "yes" ? "</DIV>\n" : "");
print LOG <<EOF;
<html> …
$ali_start<p>$txt</p></font>$ali_end
… </html>
EOF
关于一般Perl风格,请关注Eric Strom's advice。