Reputation: 125
I wrote a sleep
in a while
loop. It's a very simple code but didn't work.
use strict;
use warnings;
&main();
sub main()
{
print "hello\n";
while(1) # #1
{
print "go~ ";
sleep 2;
}
}
If commenting out #1, "go~" is printed; otherwise, it is just waiting without any "go~" to print. My intention is to do something periodically.
Could anyone give some explanation/hint?
Upvotes: 5
Views: 1829
Reputation: 1
#!/usr/bin/perl
$dir = "/home/tazim/Videos/perl/*";
my@files = glob($dir);
foreach(@files){ print $_."\n";
sleep(2);
}
Upvotes: -1
Reputation: 4868
Try adding new line after go~
use strict;
use warnings;
&main();
sub main()
{
print "hello\n";
while(1) # #1
{
print "go~\n";
sleep(2);
}
}
Explanation why it works: The stdout stream is buffered, so will only display what's in the buffer after it reaches a newline (or when it's told to). You didn't use newline so the the text will get appended to buffer until the size of buffer.
If you don't want to use newline than add following lines at begining
use IO::Handle;
STDOUT->autoflush(1);
Upvotes: 8