Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Perl | sleep() Function

  • Difficulty Level : Basic
  • Last Updated : 07 May, 2019

sleep() function in Perl is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds or forever if parameter is not specified. The sleep( ) function accepts seconds as a parameter and returns the same on success.

Syntax: sleep(seconds)

Returns: seconds passed to it as parameter, on success

Example 1:




#!/usr/bin/perl -w
  
# Using localtime() function
# to print the time
print scalar localtime();
  
# calling the sleep function
sleep(5);
  
print "\n";
  
print scalar localtime();
Output:
Thu Mar 28 06:02:21 2019
Thu Mar 28 06:02:26 2019

Example 2:




#!/usr/bin/perl -w
  
# Using localtime() function
# to print the time
print scalar localtime();
  
# Using rand() to generate random delay
sleep(rand(7));
  
print "\n";
  
print scalar localtime();
Output:
Thu Mar 28 06:02:26 2019
Thu Mar 28 06:02:27 2019
My Personal Notes arrow_drop_up
Recommended Articles
Page :