【问题标题】:Perl output shows on the left side of terminal?Perl 输出显示在终端的左侧?
【发布时间】:2012-07-30 07:11:24
【问题描述】:

奇怪的是,当我在 Perl 中执行代码时,输​​出总是出现在命令行的左侧。例如。

admin@admin-machine:~$ perl my_program
1 2 3 4 5 admin@admin-machine:~$

如何让它像下面这样在一行上显示输出?

admin@admin-machine:~$ perl my_program
1 2 3 4 5
admin@admin-machine:~$

【问题讨论】:

  • 你永远不会得到那个输出(没有手动定位光标)。提示右侧的内容是用户输入的内容。
  • 从技术上讲,这是可能的:perl -we 'fork and exit; sleep 1; print "$_ " for 1..5; '

标签: linux perl ubuntu-10.04


【解决方案1】:

您需要在打印语句的末尾添加一个换行符 (\n)。例如。

print "1 2 3 4 5 \n";

【讨论】:

  • 或者直接使用say "1 2 3 4 5";
【解决方案2】:

假设你真的知道自己在做什么,一切皆有可能。

if(0 == fork()) {
        sleep(1);
        print "1 2 3 4 5";
}

s0me0ne@ws:/tmp$ perl print.pl 
s0me0ne@ws:/tmp$ 1 2 3 4 5

我怀疑它是否对某人有帮助,但是...只是回答;)

【讨论】:

  • 请注意,这回答了最初提出的字面问题。但是,我将问题修复为与接受的答案相对应,并投票赞成这个答案。我留下这张纸条是为了避免其他人反对它。
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
相关资源
最近更新 更多