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

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

问题描述

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

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

他说现在的输出是什么?我不了解睡眠功能,我猜想在每次迭代时它将打印点(.),然后将等待1秒钟(由于睡眠功能),然后再次迭代,然后再次打印(.),然后将等待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(Higher-Order Perl的作者)在1998年左右发表的《 Perl Journal》上的出色文章.这篇文章可能已有十多年的历史了,但今天的话题和他写这篇文章时一样重要.

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

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

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