可能是失控的多行“”字符串开头

发布于 2024-11-08 20:18:05 字数 2681 浏览 0 评论 0 原文

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

请问这有什么问题吗?

syntax error at HTMLmaker.pl line 54, near ". if"
(Might be a runaway multi-line "" string starting on line 47)
Execution of HTMLmaker.pl aborted due to compilation errors.

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

Whats wrong with this please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤芳又自赏 2024-11-15 20:18:05

Perl 中的 if 语句不能是表达式的一部分,它是一个独立的构造。如果要将其放置在表达式内,则必须将其包装在 do {...} 块中。

内联等效项是 COND ? EXPR1 : EXPR2 构造,与 do {if (COND) {EXPR1} else {EXPR2}} 相同。

您应该使用用 my 声明的词法变量,而不是 use vars 编译指示。

您的代码还包含大量重复。 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;

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 a do {...} block.

The inline equivalent is the COND ? EXPR1 : EXPR2 construct which is the same as do {if (COND) {EXPR1} else {EXPR2}}.

You should use lexical variables declared with my rather than the use 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:

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;
单身狗的梦 2024-11-15 20:18:05

字符串表达式的中间有一条打印指令。这在两个方面是错误的:

  • 在 Perl 中,您不能直接将指令(做某事的命令)嵌入到表达式(如何构建值的描述)中。
  • 打印指令 print "
    " 会将该文本打印到标准输出,而不是将其包含在您要打印到 LOG 的内容中。

代码有问题的部分是这样的:

print LOG "<html> …
". if ($ali eq "yes") { print "<DIV ALIGN=CENTER>"}; ."
… </html>";

if 不能在表达式中使用,但您可以使用 条件运算符 条件 ? if_true : if_false 相反。

print LOG "<html> …
" . ($ali eq "yes" ? "<DIV ALIGN=CENTER>" : "") . "
… </html>";

另一个在这部分代码中有用的 Perl 功能是 “here-document ” 语法。这是编写多行字符串的更简单的方法。您将 < 占位符作为 print 的参数,并且直到仅包含 EOF 的行都成为一个长字符串。 @{[…]} 习惯用法 put 变量表达式解释起来有点棘手;如果这让您担心,请预先将可变部分放入变量中,然后使用这些变量。

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

遵循 Eric Strom 关于一般 Perl 的建议风格,也是如此。

You have a printing instruction in the middle of a string expression. This is wrong on two counts:

  • In Perl, you can't directly embed an instruction (an order to do something) inside an expression (a description of how to build up a value).
  • The printing instruction print "<DIV ALIGN=CENTER>" would print that text to standard output, not include it in what you're printing to LOG.

The problematic part of your code is this:

print LOG "<html> …
". if ($ali eq "yes") { print "<DIV ALIGN=CENTER>"}; ."
… </html>";

if can't be used in an expression, but you can use the conditional operator condition ? if_true : if_false instead.

print LOG "<html> …
" . ($ali eq "yes" ? "<DIV ALIGN=CENTER>" : "") . "
… </html>";

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 to print, and all the lines until one that contains just EOF 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.

print LOG <<EOF;
<html> …
@{[$ali eq "yes" ? "<DIV ALIGN=CENTER>" : ""]}
<p>$txt</p></font>
@{[$ali eq "yes" ? "</DIV>" : ""]}
… </html>
EOF

or

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

Follow Eric Strom's advice on general Perl style, as well.

几味少女 2024-11-15 20:18:05

您无法像以前那样内联 if 语句。
".if ($ali eq "yes") { print "…"}; ."…"; close (LOG); ;
==>

";
if ($ali eq "yes") { print LOG "…"};
print LOG "…";
close (LOG);

You cannot inline your if statement as you did.
". if ($ali eq "yes") { print "…"}; ."…"; close (LOG); ;
==>

";
if ($ali eq "yes") { print LOG "…"};
print LOG "…";
close (LOG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文