Christopher Peterson
Christopher Peterson

Reputation: 1027

How can I pause program execution for one second in Perl?

Could anyone show me an example of how to pause for one second in Perl? I am trying the sleep command and it isn't working.

Upvotes: 4

Views: 48854

Answers (4)

hexicle
hexicle

Reputation: 2187

This is the direct answer, which I provide just in case there was some problem with the asker's original code.

#!/usr/bin/perl
 print "Hello World.\n";
 sleep 1;

Upvotes: 2

Hugmeir
Hugmeir

Reputation: 1259

Why you guys, that's obviously not the way.

select undef, undef, undef, 1;

Upvotes: 3

cdhowie
cdhowie

Reputation: 169038

sleep(1) will do exactly that. If it's "not working" then you are measuring something incorrectly. (That or you're not passing the right arguments -- showing us your code would be helpful.)

$ time perl -e 'sleep(1)'

real    0m1.003s
user    0m0.000s
sys     0m0.004s

Upvotes: 14

Quentin
Quentin

Reputation: 943625

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

$| = 1; # Disable output buffering

for (1..10) {
        print '.';
        sleep 1;
}
print "\n";

Upvotes: 16

Related Questions