Perl 无法正确打印

发布于 2024-11-09 21:43:43 字数 385 浏览 6 评论 0原文

好的,所以我有一些类似于您在下面看到的子例程,我的问题是打印函数在实际命令完成之前不会打印出来,我希望它打印
“Has MySQL,正在安装:”,然后执行命令然后打印 OK。我已经尝试过使用 sleep 并尝试清除 $ssh_d 对象。任何建议表示赞赏。不要担心这个特定子系统中的变量,问题到处都在发生。谢谢你们。

if ($MySQL)
{
    print "Has MySQL, Installing: ";
    $mysqlCmd = "/path/to/script/mysql-install.pl $person > /dev/null 2>&1";
    $ssh_d->cmd("$mysqlCmd");
    print "OK\n";
}

Ok, so I have some sub routines similar to what you see below, my issue is that the print function is not printing out until the actual command is complete, I want it to print
"Has MySQL, Installing:", and then do the command then print OK. I have already tried using sleep and tried clearing the $ssh_d object. Any advice appreciated. Don't worry about the variables in this particular sub, the issue is happening all over. Thanks guys.

if ($MySQL)
{
    print "Has MySQL, Installing: ";
    $mysqlCmd = "/path/to/script/mysql-install.pl $person > /dev/null 2>&1";
    $ssh_d->cmd("$mysqlCmd");
    print "OK\n";
}

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

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

发布评论

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

评论(2

手心的海 2024-11-16 21:43:43

STDOUT 通常是行缓冲的,因此在打印换行符之前您不会看到输出。您可以在打印之前设置 $| 以强制打印输出。

要在每次打印后打开自动刷新,请将 $| 设置为 1:(

$| = 1;
print "Enter a number between 3 and 5: ";
chomp( my $answer = <STDIN> );

请注意,$| 实际上并不是单个值,它是针对每个文件句柄单独跟踪的。设置它时,它会影响当前选定文件句柄的设置(请参阅选择)。

STDOUT is usually line-buffered, so you won't see your output until you print a newline. You can set $| before printing to force your output to be printed.

To turn on autoflushing after each print, set $| to 1:

$| = 1;
print "Enter a number between 3 and 5: ";
chomp( my $answer = <STDIN> );

(Note that $| isn't actually a single value, it is tracked separately for each filehandle. When you set it, it affects the setting for the currently selected filehandle (see select).

冬天旳寂寞 2024-11-16 21:43:43

谢谢!你能给我看一个如何使用 $| 的例子吗?我不熟悉,这个地方永远不会失败哈哈 –

这个可以吗?

$| = 1;
print "This is now unbuffered printing. "
sleep 3;
print "You no longer have to wait for the NL character. ";
sleep 3;
print "\n";

Thanks! Can you show me an example of how to use $| I am unfamiliar, this place never fails lol –

Will this do?

$| = 1;
print "This is now unbuffered printing. "
sleep 3;
print "You no longer have to wait for the NL character. ";
sleep 3;
print "\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文