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

my $duration = 1000000; # in seconds my $wait = $duration; while ($wait >= 0) { $cmd1; $cmd2; $cmd3; $wait = $duration-time;#Decrement the time till it executed $cmd3 wi +th $duration }

In the above code, i want to execute the while loop for $duration seconds. Once it reaches $duration while should break. Sleep won't help here , neither the time . Is there a way to acheive this

Replies are listed 'Best First'.
Re: timer in perl
by haukex (Archbishop) on Feb 20, 2018 at 10:57 UTC
    Sleep won't help here , neither the time .

    I'm not sure what you mean? This should work:

    my $work_until_time = time+10; print gmtime."\n"; # Debug while ( time<$work_until_time ) { ... } print gmtime."\n"; # Debug
Re: timer in perl
by hippo (Archbishop) on Feb 20, 2018 at 11:10 UTC

    What are $cmd1, $cmd2, $cmd3? If they are blocking processes you'll probably want some sort of sub-process control, perhaps threads, perhaps forks. Maybe you just want alarm?

    If they aren't blockers then it isn't clear where the problem lies. As usual, an SSCCE would be best.

Re: timer in perl
by LanX (Saint) on Feb 20, 2018 at 10:59 UTC
    before the loop

     $start = time;

    at the end of the loop

    last if time > $start + $duration;

    (untested)

    OR

     while ( time < $start + $duration)

    instead of an endless loop.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Re: timer in perl
by thanos1983 (Parson) on Feb 20, 2018 at 11:57 UTC

    Hello Anonymous Monk,

    Fellow Monks have already provided you with ideas, but since we do not have a complete idea on what you are trying to do maybe you are looking for something like that Re^3: script should wait for another script to complete?

    From my point of you, I think that you are looking to execute a process first and then run the remaining commands. You do not need to wait for an arbitrary time, just keep track of when the command / process / script is finished and then execute the rest. On the sample of the link above you can do that.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: timer in perl
by karlgoethebier (Abbot) on Feb 20, 2018 at 16:59 UTC

    Probably you want something like System::Timeout?

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: timer in perl
by clueless newbie (Curate) on Feb 20, 2018 at 13:56 UTC
    Seems to a clueless newbie that he wants to time out $cmd1, $cmd2 and $cmd3. If that's the case he might want to look at Time::Out.
Re: timer in perl
by Anonymous Monk on Feb 20, 2018 at 16:53 UTC
    Your while strategy is sound in concept but you can't count on decrement to do the right thing. Before the loop begins, calculate the deadline after which your loop should end, and then test within the loop to see if that deadline has now been passed. But take care that you are not "busy waiting."