简体   繁体   English

Perl:do-while循环后打印错误

[英]Perl: Error printing after do-while loop

I am making a concatenating do-while loop wherein each of the entered strings will be added to the main string $food . 我正在建立一个串联的do-while循环,其中每个输入的字符串都将添加到主字符串$food Here is the code: 这是代码:

do {
  print("\nEnter your order(-1 to end): ");
  $order = <>;
  chop($order);

  if ($order != -1) {
   $food .= " ".$order;
   print($food);
  }  
} while ( $order != -1)

print ($food); #PROBLEM HERE!

The problem is that whenever I put print ($food) outside the loop , a syntax error for this line appears plus an Execution of file.pl aborted due to compilation errors message. 问题是,每当我将print ($food)放入循环外时,都会出现此行的语法错误,以及Execution of file.pl aborted due to compilation errors消息Execution of file.pl aborted due to compilation errorsExecution of file.pl aborted due to compilation errors

The code works once I put print ($food) inside the loop but I am curious to why this error is happening. 一旦将print ($food)放入循环,代码便可以工作,但我很好奇为什么会发生此错误。

Your line before last print has syntax error - it does not end with ; 您上次打印之前的行存在语法错误-它不以;结尾; . Just add it and it should work. 只需添加它,它应该可以工作。

A thing worth mentioning in addition to the syntax error: I see that you you start the string with a newline. 除了语法错误外,还有一件事值得一提:我看到您使用换行符开始字符串。 While this is fine, it might be considered "better" to end strings with the newline before printing, as printing a newline would result in flushing the STDOUT buffer right away. 虽然这很好,但在打印之前用换行符结束字符串可能会被认为“更好”,因为打印换行符会导致立即刷新STDOUT缓冲区。 In most cases, this is the behavior you want, as it prevents delayed output and text flickering (especially if printing a lot). 在大多数情况下,这是您想要的行为,因为它可以防止延迟输出和文本闪烁(尤其是打印大量内容时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM