use warnings; use strict; use feature 'say'; # simulate the light and times db entries my $light = 0; my $on = '18:00'; my $hrs = 12; my $now = time; my ($start, $end); # the following will be run once only when # the main webapp is started. This sets the # initial lights-on/off times into the db. # of course, it'll just overwrite without first # checking if the time was set previously or not if (! $start){ set_times(); $start = $start - 24 * 3600; $end = $end - 24 * 3600; } # simulate event calling the light method action_light(); say "light is on" if $light; # object method that the event calls sub action_light { if ($now > $start && $now < $end){ if (! $light){ $light = 1; } } elsif ($light) { $light = 0; set_times(); } } # obj method called by action_light() and upon # main webapp startup sub set_times { my $time = time; $time += 60 until localtime($time) =~ /$on:/; $start = $time; $end = $start + $hrs * 3600; }