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

Hi Monks!

I need to process some records ( 10000 or more ) for this reason I am trying to make the code in the "foreach loop" to "sleep" for 10 seconds when the remainder of count reaches 100, can't think straight right now and this is the sample code I have:
... my $count = 0; foreach my $rec ( @{$names} ) { $count++; sleep(10) if $count / 10 == 100; ... } ...
thanks for helping!

Replies are listed 'Best First'.
Re: Sleep and Perl
by toolic (Bishop) on Dec 15, 2015 at 20:17 UTC
    I think you want the modulo operator (perlop)
    sleep(10) if $count % 100 == 0;
    UPDATE: fixed typo (choroba++)

      Hello toolic,

      You may never get to the end of the file if you get more than 100 entries within 10 seconds. I believe the test needs to know the size of the file or the number of records to start with, otherwise you'll never know when you have xx records left.

      If you know that each record average 100 bytes then...(untested)

      my $size = -s $file; my $avg_recsize = 100; while my $record ( <$in> ) { if ( ( $size - ( 100 * $avg_recsize ) ) <= 0 ) { sleep(10); $size = -s $file; } . . . }
      I think this is similar to the 'pop before email' technique. YMMV.

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

        Hmmm ... Can you explain further. My read of the OP does not show any problems with getting to the end of the loop -- there's nothing in the body of the loop to prevent the eventual exit.

        -derby