How do I sleep for a millisecond in Perl?

How do I sleep for shorter than a second in Perl?


  • From the Perldoc page on sleep:

    For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides usleep().

    Actually, it provides usleep() (which sleeps in microseconds) and nanosleep() (which sleeps in nanoseconds). You may want usleep(), which should let you deal with easier numbers. 1 millisecond sleep (using each):

    use strict;
    use warnings;
    use Time::HiRes qw(usleep nanosleep);
    # 1 millisecond == 1000 microseconds
    # 1 microsecond == 1000 nanoseconds

    If you don't want to (or can't) load a module to do this, you may also be able to use the built-in select() function:

    # Sleep for 250 milliseconds
    select(undef, undef, undef, 0.25);