fz315 has asked for the wisdom of the Perl Monks concerning the following question:

Hi: anyone know how to pause 10ms in perl script? sleep command just pause seconds. Thanks a lot.

Replies are listed 'Best First'.
Re: How to pause 10ms in perl
by legato (Monk) on Jan 25, 2005 at 19:31 UTC
    If you use Time::HiRes and export its sleep, you can sleep in fractional seconds. A 10ms sleep would work like this:
    #!/usr/bin/perl -w use strict; use Time::HiRes ('sleep'); print STDERR 'Pausing:'; sleep(0.01); ## Sleep for 1/100th of a second (10ms) print STDERR 'done',"\n";

    It's worth mentioning that this isn't guaranteed to be ultra-accurate.

    Anima Legato
    .oO all things connect through the motion of the mind

Re: How to pause 10ms in perl
by sweetblood (Prior) on Jan 25, 2005 at 19:24 UTC
Re: How to pause 10ms in perl
by davis (Vicar) on Jan 25, 2005 at 19:25 UTC
    According to perldoc -f sleep:

    For delays of finer granularity than one second, you may use
    Perl's "syscall" interface to access setitimer(2) if your sys-
    tem supports it, or else see "select" above. The Time::HiRes
    module (from CPAN, and starting from Perl 5.8 part of the stan-
    dard distribution) may also help.


    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: How to pause 10ms in perl
by premchai21 (Curate) on Jan 25, 2005 at 19:28 UTC

    From the docs for sleep (with minor reformatting):

    For delays of finer granularity, you may use Perl's syscall interface to access setitimer(2) if your system supports it, or else see select above. The Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) may also help.

    Following the reference to select, we find:

    You can effect a sleep of 250 milliseconds this way:

    select(undef, undef, undef, 0.25);

    Note that whether select gets restarted after signals (say, SIGALRM) is implementation-dependent.

    Please try reading the perldocs before asking questions here.

Re: How to pause 10ms in perl
by Anonymous Monk on Jan 25, 2005 at 20:52 UTC

    I always use

    for(1 .. 10000) { 1; }

    ...what, why is everyone staring at me?

      Wassercrats?!