Perl | sleep() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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();

chevron_right


Output:

Thu Mar 28 06:02:21 2019
Thu Mar 28 06:02:26 2019

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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();

chevron_right


Output:

Thu Mar 28 06:02:26 2019
Thu Mar 28 06:02:27 2019


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.