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

hey monks, i got a super simple question for you. is there any way to insert a short multi-second delay into my code? I need my program to pause for 2 or 3 seconds before moving onto the next line. if not, does anyone know a unix system call that could do it for me? thanks londonblue007

2006-11-01 Retitled by GrandFather, as per Monastery guidelines
Original title: 'simple question'

  • Comment on How do I add a delay in my perl program?

Replies are listed 'Best First'.
Re: How do I add a delay in my perl program?
by liverpole (Monsignor) on Nov 01, 2006 at 18:46 UTC
    Hi londonblue007,

    Very simply ... sleep!


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: How do I add a delay in my perl program?
by bobf (Monsignor) on Nov 01, 2006 at 18:47 UTC

    Something like sleep, perhaps? For more fun, try the 4-argument form of select.

Re: How do I add a delay in my perl program?
by andyford (Curate) on Nov 01, 2006 at 22:43 UTC
    That (sleep) being said, I would caution you to not repeat the mistake I've seen many people make: using a sleep that works most of the time, but fails when things get busy.
    Here's the scenario: you've got a process you want to wait for and it takes a couple seconds. You put the sleep in and everything's fine till one day the process takes ten seconds for some reason. Your assumptions go to heck and your system fails.
    If this is your possible scenario, try to look for ways to know for sure if the thing you're waiting for is done. Waiting for a return code. Or maybe a wrapper that runs process A, waits on it and then runs process B only when A is really done.

    andyford
    or non-Perl: Andy Ford

Re: How do I add a delay in my perl program?
by Zaxo (Archbishop) on Nov 02, 2006 at 08:45 UTC

    If sleep is interrupted by another signal, your program won't know unless it checks:

    my $delay = 5; $delay -= sleep $delay while $delay > 0;

    After Compline,
    Zaxo

Re: How do I add a delay in my perl program?
by davido (Cardinal) on Nov 02, 2006 at 00:25 UTC

    sleep usage:

    while ( not defined $result ) { ( $result = try( $something ) ) or sleep 3; }

    And a select example:

    while ( not defined $result ) { ( $result = try( $something ) ) or select undef, undef, undef, 3000; }

    select can be used to pause for n number of milliseconds, so 3000 would be three seconds. Though this isn't the primary purpose for select, it's a useful side effect. What's useful about it is that you have more resolution than with sleep (which only has one-second granularity).

    Probably a more Perlish solution than abusing select would be to use Time::HiRes (which I believe comes with Perl). Its use is as simple as sleep:

    use Time::HiRes qw/ usleep /; while( not defined $result ) { ( $result = try( $something ) ) or usleep( 3000 ); }

    Dave

Re: How do I add a delay in my perl program?
by swampyankee (Parson) on Nov 01, 2006 at 23:09 UTC

    Kudos to andyford for his comments. Yes, as was said sleep, or the Time::HiRes version of it will sleep for $n seconds. It may also be a bad idea. If you're program is waiting for a process it kicks of to finish, it may be better to have the process send a signal to your program.

    Why do you want your program to wait a few seconds? Tell us that and you'll probably get a much better answer than 'use sleep.'

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: How do I add a delay in my perl program?
by Joost (Canon) on Nov 01, 2006 at 20:28 UTC