Perl

Perl Sleep() Function

Sometimes, it is required to stop the execution of the script for a certain period for programming purposes. The sleep() function is used for this task in Perl. It is a built-in function of Perl that can be used to stop the execution of the script for a particular number of seconds by default or for unlimited times when it is used without any argument. The uses of this function for different types of tasks are shown in this tutorial using multiple examples.

Syntax:

The syntax of the sleep() function is given as follows:

or

sleep ( number)
  • If this function is used without any argument, the script waits for unlimited times.
  • If this function is used with the argument value, the script waits for the particular times that are mentioned in the argument.

Different Examples of Perl the Sleep() Function

Different uses of the sleep() function are shown in this part of the tutorial.

Example 1: Read the Date and Time with Sleep() Function

Create a Perl file with the following script that prints the current system time with the three seconds duration using the sleep() function. Here, the localtime() function is used to read the time value in a human readable format.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Read the current timestamp value
my $start = time();
#Read the time in human-readable format
(my $sec, my $min, my $hour) = localtime($start);
#Print the current time
print "Time before calling the sleep() function is $hour:$min:$sec\n";

#Wait for three seconds
sleep(3);

#Read the current timestamp after 3 seconds again
my $end = time();
#Read the time in human-readable format
($sec, $min, $hour) = localtime($end);
#Print the current time
print "Time after calling the sleep() function is $hour:$min:$sec\n";

#Calculate the time duration
my $duration = $end - $start;

#Print the time duration
print "The script was paused for $duration seconds.\n";

Output:

The following output appears after executing the script. According to the output, the duration between two times is 3 seconds and the sleep() function is used in the script to wait for 3 seconds:

Example 2: Wait for a Certain Period of Time while Printing the Output

Create a Perl file with the following script that prints each character of a string with a one second delay. The split function is used here to read each character from the input value that is taken from the user. The “foreach” loop reads each character, prints the character, and waits for one second until all characters of the string value are printed.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Disable buffering
$| = 1;

print "Enter your favorite color: ";
#Take input from the user
my $color = <>;
#Remove the newline
chomp($color);
#Count the length of the string
my $len = length($color);

#Check the length of the string
if ($len >= 1)
{
      print "Your favorite color:";
      # Print each character in the string
      foreach my $c (split //, $color)
      {
            #Print each character of the string
            print "$c ";
            #Wait for 2 second
            sleep(1);
      }
      print "\n";
}
else
{
     #Print a message for the user
     say "No input is given.";
}

Output:

The following output appears after executing the script:

Example 3: Wait for a Random Period

Create a Perl file with the following script that takes an answer from the user and check whether the answer is correct or incorrect after waiting for a random period. The rand() function is used in the script to generate a random number between 0 to 5 and waits for a particular period based on a random value. Next, the script prints a message based on the input value that is taken from the user.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Print a question for the user
say "What is the result of 5 + 3?";
#Take input
my $answer = <>;
#Remove newline
chomp($answer);

#Generate a random number
my $random = int(rand(5));
say "Wait for $random seconds...";

#Wait for a certain period
sleep($random);
#Check the answer
{
         say "Your answer is correct.";
}
else
{
         say "Your answer is incorrect.";
}

Output:

The following output appears after executing the script if the correct input value is provided. Here, 2 is generated as the random number and the script is paused for 2 seconds:

The following output appears after executing the script if the incorrect input value is provided. Here, 1 is generated as the random number and the script is paused for 1 second:

Conclusion

Sleep() is a very useful function of Perl to stop or pause the execution of the program for a certain period. Perl users will be able to use this function on their script after reading this tutorial properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.