I've installed plenty of Perl scripts but can't write them - even simple ones like this probably is...
I'm looking for a server side script that looks at the current time and day of week and loads one of two images. The idea is that for a different hour each day the site owner takes calls, so one graphic will say to "call now" the other "sorry phones are closed". For example: Monday 9:00 - 10:00, Tuesday 10:00 - 11:00, etc. He has a seven day schedule that is the same week to week. Ideally, the "phones closed" image will also load some javascript to pop open a window with the weekly schedule.
Short of the javascript twist, I know there's not much challenge for a Perl programmer, but all the scripts I've come across don't quite do it.
Anyone know of a script to accomplish this and if it can have the javascript twist added to it?
Thanks guys!
8:30am = 30600, (or 60 x 60 x 8.5)
8:35am = 30900, (or 60 x 60 x 8) + (60 x 35)
5:30pm = 63000, (or 60 x 60 x 17.5)
8:30pm = 73800, (or 60 x 60 x 20.5)
12noon = 43200, (or 60 x 60 x 12)
Here's a quick and dirty approach. (You would need to use SSI and include this file into the page where you want the "Phone On" or "Phone Off" image to appear).
#!/usr/local/bin/perl
#
# set the time for phone to be 'ON' and 'OFF' below
#
$phoneon = '30600';
$phoneoff = '63000';
#
# edit the names of the images to use below
#
$image1 = "phone-on.gif";
$image2 = "phone-off.gif";
#
#
$image = $image2;
($h,$m) = (localtime)[2,1];
$t = ($h * 3600) + ($m * 60);
if (($t > $phoneon) && ($t < $phoneoff)) {
$image = $image1;
}
#
print "Content-type: text/html\n\n";
print "<img src=\"$image\"><br>\n";
#
# end
You can build the script to handle different days and times using the values from localtime[] returned values.
# =================================================================
# localtime
# =================================================================
# # ¦ Variable ¦ Values ¦ Range
# =================================================================
# 0 ¦ $sec ¦ seconds ¦ 0-60
# 1 ¦ $min ¦ minutes ¦ 0-59
# 2 ¦ $hours ¦ hours ¦ 0-23
# 3 ¦ $mday ¦ day of month ¦ 1-31
# 4 ¦ $month ¦ month of year ¦ 0-11, 0 == January
# 5 ¦ $year ¦ years since 1900 ¦ 1-138 (or more)
# 6 ¦ $wday ¦ day of week ¦ 0-6, 0 == Sunday
# 7 ¦ $yday ¦ day of year ¦ 1-366
# 8 ¦ $isdst ¦ 0 or 1 ¦ 1 if daylight savings in effect
# =================================================================
# usage: ($var,$var,$var) = (localtime)[#,#,#];
# =================================================================
#
use strict;
use warnings;
# time in military format 0-23
# first number is opening time
# second number is closing time
# 24-24 indicates always closed
my %schedule = (
Sun => [24,24],
Mon => [9,10],
Tue => [10,11],
Wed => [9,11],
Thu => [8,10],
Fri => [13,15],
Sat => [24,24]
);# default value
my $image = 'closed.gif';# get the day and hour
my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
$hour = (split(/:/,$hour))[0];# check the day and hour
if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] >= $hour) {
$image = 'open.gif';
}# print the image
print "<img src='$image'>";
<img src="cgi-bin/phone.pl" width="50" height="50" alt="image of a phone">
the phone.pl script:
#!/usr/bin/perl
use strict;
use warnings;# path to folder where images are
# assumes an image named open.gif and closed.gif
my $path = '/home/yoursite/images/';# time in military format 0-23
# first number is opening time
# second number is closing time
# 24-24 indicates always closed
my %schedule = (
Sun => [24,24],
Mon => [9,10],
Tue => [10,11],
Wed => [9,11],
Thu => [8,10],
Fri => [13,15],
Sat => [24,24]
);# default value
my $image = 'closed.gif';# get the day and hour
my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
$hour = (split(/:/,$hour))[0];# check the day and hour
if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] >= $hour) {
$image = 'open.gif';
}#print appropriate MIME type header
print "Content-type: image/gif\n\n";open(FH,"$path$image") or die "$!";
binmode(FH);
print while (<FH>);
close(FH);
exit;
as far as the javascript pop-up with a calendar goes I would forget about that because pop-up blockers will just block it. Have a link the user can click on if they want to see a calendar.
I've now tried the version from perl_diver and I can't get that to work. I changed the path to perl to #/usr/bin/perl I changed the path to the images to my $path = '../images/'; I set the script to 755. The html page is in the root level. File names are all lower case. I set the image names and put them in the images folder and still it doesn't work! Anyone know what I'm doing wrong?
I'm assuming the path to the images is relative to the script and not to the page calling them right? That would mean from the cgi-bin it would have to hop up one level to the root and then into the images folder as I have it right?
Thanks guys!
You may need to add the following to your .htaccess file (or create an .htaccess file if one does not exist).
AddType text/html .shtml
AddHandler server-parsed .htm
AddHandler server-parsed .html
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
use CGI::Carp qw/fatalsToBrowser/;
then run the script directly from the url of the script:
http://www.example.com/cgi-bin/phone.pl
and see what happens.
You can see the script in action here:
that is the script I posted called directly from the URL of the script but it would work the same if called from an <img> tag in an html document:
<img src="cgi-bin/phone.pl">
My guess is that your script is "dieing" here:
open(FH,"$path$image") or die "$!";
and trying to print the error message contained in $!
[edited by: jatar_k at 12:46 pm (utc) on June 15, 2007]
[edit reason] sorry no urls thanks [/edit]
To prove I can follow the basic logic, am I right in believing that any matching time would work as the "closed time" and not just 24,24 such as 03,03?
Not that I need it, but so I can understand more: it seems to me that if the script had to be changed to work with minutes like open 11:30 - 4:45, it wouldn't take much to do it other than adding a function to get minutes too, right?
Mon => [10,10],
Monday hours (above) are 10 AM to 10 AM (10 to 10).
We check time (below) and the hour is 10.
# default value
my $image = 'closed.gif';# get the day and hour
my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
$hour = (split(/:/,$hour))[0];
Now check to see if we are in the open/closed range:
# check the day and hour
if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] >= $hour) {
$image = 'open.gif';
}
it will look like this:
if (10 <= 10 && 10 >= 10) {
which translates to:
if 10 is equal or less than 10 and 10 is equal or greater than 10
which is true because 10 is "equal" to 10 on both sides of the "and" operator. So for 59 minutes and 59 seconds the hours of 10,10 will display as "open". I used 24,24 because the return value of "localtime" will never be 24. So one side or the other of the line that checks the time range will always be false and "open" will never be returned. I hope that makes sense.
Not that I need it, but so I can understand more: it seems to me that if the script had to be changed to work with minutes like open 11:30 - 4:45, it wouldn't take much to do it other than adding a function to get minutes too, right?
Thats right, you would just need to add in the minutes. It would not be hard.