可能是失控的多行“”字符串开头
HTMLmaker.pl 第 54 行“. 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>;
请问这有什么问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl 中的
if
语句不能是表达式的一部分,它是一个独立的构造。如果要将其放置在表达式内,则必须将其包装在do {...}
块中。内联等效项是
COND ? EXPR1 : EXPR2
构造,与do {if (COND) {EXPR1} else {EXPR2}}
相同。您应该使用用
my
声明的词法变量,而不是use vars
编译指示。您的代码还包含大量重复。 Perl 非常擅长让您消除这种情况,从而节省时间、简化重构并减少错误。
例如,将颜色名称映射到十六进制代码应该使用哈希表来完成。类似地,在提示用户后设置许多变量可以编写为循环。
这是我的意思的一个简短示例:
The
if
statement in Perl can not be part of an expression, it is a stand alone construct. If you want to place it inside of an expression, it must be wrapped in ado {...}
block.The inline equivalent is the
COND ? EXPR1 : EXPR2
construct which is the same asdo {if (COND) {EXPR1} else {EXPR2}}
.You should use lexical variables declared with
my
rather than theuse vars
pragma.Your code also contains a good deal of repetition. Perl is very good at letting you eliminate this, which saves time, eases refactoring, and cuts down on errors.
For example, mapping color names to hex codes should be done with a hash table. Similarly, setting many variables after prompting the user could be written as a loop.
Here is a short example of what I mean:
字符串表达式的中间有一条打印指令。这在两个方面是错误的:
print "
"
会将该文本打印到标准输出,而不是将其包含在您要打印到LOG
的内容中。代码有问题的部分是这样的:
if
不能在表达式中使用,但您可以使用 条件运算符条件 ? if_true : if_false
相反。另一个在这部分代码中有用的 Perl 功能是 “here-document ” 语法。这是编写多行字符串的更简单的方法。您将
< 占位符作为
print
的参数,并且直到仅包含EOF
的行都成为一个长字符串。@{[…]}
习惯用法 put 变量表达式解释起来有点棘手;如果这让您担心,请预先将可变部分放入变量中,然后使用这些变量。或者
遵循 Eric Strom 关于一般 Perl 的建议风格,也是如此。
You have a printing instruction in the middle of a string expression. This is wrong on two counts:
print "<DIV ALIGN=CENTER>"
would print that text to standard output, not include it in what you're printing toLOG
.The problematic part of your code is this:
if
can't be used in an expression, but you can use the conditional operatorcondition ? if_true : if_false
instead.Another Perl feature that would be useful in this part of the code is the “here-document” syntax. It's a simpler way of writing multiline strings. You put a
<<EOF
placeholder as the argument toprint
, and all the lines until one that contains justEOF
become one long string. The@{[…]}
idiom to put a variable expression is a little trickier to explain; if this worries you, put the variable parts in variables beforehand and just use those variables.or
Follow Eric Strom's advice on general Perl style, as well.
您无法像以前那样内联
if
语句。".if ($ali eq "yes") { print "…"}; ."…"; close (LOG); ;
==>
You cannot inline your
if
statement as you did.". if ($ali eq "yes") { print "…"}; ."…"; close (LOG); ;
==>