珀尔 |睡眠()函数
Perl 中的 sleep()函数是一个内置函数,用于将当前脚本的执行延迟指定的秒数或如果未指定参数则永远延迟执行。 sleep()函数接受秒作为参数并在成功时返回相同的值。
Syntax: sleep(seconds)
Returns: seconds passed to it as parameter, on success
示例 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();
输出:
Thu Mar 28 06:02:21 2019
Thu Mar 28 06:02:26 2019
示例 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();
输出:
Thu Mar 28 06:02:26 2019
Thu Mar 28 06:02:27 2019