Perl 24 Hour Sleep Clock

I searched a lot for all kinds of sleep and wake solutions to find everything but what I wanted. Hopefully this helps someone else.

Problem: based on a 24hour clock, sleep after 5pm and wake (go back to work) at 8am. Or pause during non-work hours.

#!/usr/bin/perl

use strict;
use warnings;

my $waketime = 8; # sleep when hour < 8am
my $sleeptime = 16; # sleep when hour > 5pm

my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();

while ( ( $hour < $waketime )  || ( $hour > $sleeptime ) ) {

print “Its $hour. Im sleeping until $waketime \n”;
## LOOP every 1000 seconds until 8am ##
print “Sleeping 1000 seconds and then we’ll check the time again\n”;
sleep(1000);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();

}

You may also like...