当我使用 sleep 时,为什么在循环的每次迭代中不打印输出任何内容? [英] Why doesn't print output anything on each iteration of a loop when I use sleep?

查看:31
本文介绍了当我使用 sleep 时,为什么在循环的每次迭代中不打印输出任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在我的大学里,一位老师问了我一个问题.他把这段代码写在纸上说这段代码的输出是什么?"

Today in my college a teacher asked me a question. He wrote this code on the paper and said "What will be the output of this code?"

use warnings;

for (1 .. 20)
{
    print ".";
}

我发现这很简单,并说它会循环 20 次,并且在每次迭代时会打印一个点 (.),因此总共输出 20 个点.

I found it easy and said that it will loop 20 times and at each iteration it will print a dot (.) and hence total 20 dots will be the output.

他说你是对的,然后他对代码进行了一些更改.代码是:

He said you are right and then he made some changes in the code. The code was:

use warnings;

for (1 .. 20)
{
    print ".";
    sleep 1;
}

他说现在输出是什么?我不知道 sleep 函数,我猜在每次迭代时它会打印点 (.) 然后它会等待 1 秒(因为 sleep 函数)然后它会再次迭代然后再次它会打印 (.) 然后它会等待 1 秒等等...

He said the what will be the output now? I didn't know about the sleep function, I guessed that at each iteration it will print the dot (.) and then it will wait for 1 second (because of the sleep function) and then again it will iterate and then again it will print (.) then it will wait for 1 second and so on...

老师叫我回家查一下.我在家里试过,我知道第二个代码等待 20 秒,然后一次打印所有点(20 点).我想知道这是怎么发生的?为什么点 (.) 没有在每次迭代中打印出来?

The teacher told me to check it at home. I tried it at home and I came to know that the second code waits for 20 seconds and then it prints all dots (20 dots) at once. I want to know how this happened? Why isn't the dot (.) is getting print on each iteration?

推荐答案

真正的问题与 sleep 无关,而是那个........

The real issue has nothing to do with sleep, but rather that............

正在遭受缓冲.提供的链接将您带到 Marc Jason Dominus(高阶 Perl 的作者)发表于 1998 年左右的 Perl 期刊上的一篇优秀文章.这篇文章可能已经有十多年的历史了,但这个话题在今天和他写这篇文章时一样重要.

You are Suffering from Buffering. The link provided takes you to an excellent article from The Perl Journal circa 1998 from Marc Jason Dominus (the author of Higher-Order Perl). The article may be over a decade old, but the topic is as relevant today as it was when he wrote it.

其他人已经解释了$|= 1; 技术.我要补充那些评论,在 Perl 社区的主要思想中似乎是 $|= 1$|++ 更可取,因为它的含义更清晰.我知道,自动增量也很简单,但是每个看过你代码的人都知道 $|++-- 已应用(无需在 perlvar 中查找).我碰巧也更喜欢本地化 Perl 的特殊变量"的任何修改,这样效果就不会渗透到代码的其他部分中,这些部分在对默认 Perl 行为的特定更改时可能不会很好地发挥作用.既然如此,我会把它写成:

Others have explained the $| = 1; technique. I would add to those comments that in the predominant thinking of the Perl community seems to be that $| = 1 is preferable over $|++ simply because it is clearer in its meaning. I know, autoincrement is pretty simple too, but does everyone who will ever look at your code know $|'s behavior when ++ or -- are applied (without looking it up in perlvar). I happen to also prefer to localize any modification of Perl's "special variables" so that the effects are not washing over into other portions of code that may not play nice with a particular change to default Perl behavior. So that being the case, I would write it as:

use strict;
use warnings;

{
    local $| = 1;
    for ( 1 .. 20 ) {
        print '.';
        sleep 1;
    }
}

这篇关于当我使用 sleep 时,为什么在循环的每次迭代中不打印输出任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆