📄 ch11.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Without a title - Title</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">
<H2 ALIGN="CENTER"><A HREF="ch10.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch10.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><A HREF="ch12.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch12.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif"
WIDTH="45" HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><BR>
<BR>
<FONT COLOR="#0000AA">11</FONT><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Datebooks, Calendars, and Scheduling
on the Web<BR>
</FONT>
<HR>
</H2>
<UL>
<LI><A HREF="#Heading1">Datebooks, Calendars, and Scheduling on the Web</A>
<UL>
<LI><A HREF="#Heading2">HyperCalA Modular Perl5 Calendar and Datebook</A>
<LI><A HREF="#Heading3">Listing 11.1. hypercal.cgi</A>
<UL>
<LI><A HREF="#Heading5">Configuration of HyperCalThe Variables File</A>
</UL>
<LI><A HREF="#Heading6">Listing 11.2. Variables and configuration file for HyperCal</A>
<UL>
<LI><A HREF="#Heading8">Displaying Appointments on a Specific Day: disp_day.cgi</A>
</UL>
<LI><A HREF="#Heading9">Listing 11.3. disp_day.cgi</A>
<UL>
<LI><A HREF="#Heading11">Adding Calendar Entries with add_date.cgi</A>
</UL>
<LI><A HREF="#Heading12">Listing 11.4. add_date.cgi</A>
<UL>
<LI><A HREF="#Heading14">Changing User Passwords with password.change</A>
</UL>
<LI><A HREF="#Heading15">Listing 11.5. password.change</A>
<UL>
<LI><A HREF="#Heading17">Other Supplementary Programs</A>
</UL>
<LI><A HREF="#Heading18">Summary</A>
</UL>
</UL>
<P>
<HR>
</P>
<P><TT>HyperCal--A Modular Perl5 Calendar and Datebook</TT> As the Internet and corporate
intranets become more and more widespread, and we find Netscape and other browsers
running on more and more PCs, Web-based applications that keep us organized are becoming
more prevalent. Why use a Web-based calendar or datebook? Web-based calendars and
datebooks are the ultimate way to take advantage of the Web, especially in inter-office,
or intranet environments.</P>
<P>Let's consider an example. Your boss has given your team a new project. It is
your responsibility to coordinate the schedules of each of the 10 people on your
team. Do you see where this is going? Each member could access a Web-based scheduling
mechanism, update his or her schedule, see what everyone else is doing, easily communicate
with each other, make comments, and so on. In addition to being a convenient and
efficient way to coordinate people and manage resources, once the data has been entered,
you can easily use Perl to generate reports, calculate efficiency of work cycles,
and so on. The possibilities are endless. Furthermore, using Perl and the Web, the
system is accessible anywhere Netscape is running and is totally extensible and scaleable.
You could manage and track the schedules of everyone in your entire organization.
<H3 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">HyperCalA Modular
Perl5 Calendar and Datebook</FONT></H3>
<P>In the pages that follow, we will explore HyperCal, a Calendar/Datebook program
written for Perl5 by Richard Bowen. HyperCal can be obtained from Richard Bowen's
Web site at</P>
<PRE><A HREF="javascript:if(confirm('http://www.rcbowen.com/perl/HyperCal.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.rcbowen.com/perl/HyperCal.html'" tppabs="http://www.rcbowen.com/perl/HyperCal.html"><FONT COLOR="#0066FF">http://www.rcbowen.com/perl/HyperCal.html</FONT></A><FONT
COLOR="#0066FF">
</FONT></PRE>
<P>I recommend installing HyperCal on your Web server before proceeding any further.
To install HyperCal, download the HyperCal_2.x.tar file to your server, place it
in a directory under CGI-bin, and extract the tar archive.</P>
<P>I encourage you to use your understanding of this example as a stepping stone
to implementing a complete Web-based scheduling system customized for your needs.
HyperCal consists of eight pieces of code that perform specific tasks related to
the calendar. This chapter is intended to show how Perl5 can be used to maintain
a Web-based datebook and calendar. I encourage you to look over the source code and
its output as you read through the chapter. I also encourage you to set HyperCal
up on your server and play with the source code and see how it affects the output.</P>
<P>The power of Perl lies in the capability to easily and quickly reuse and modify
existing code to suit your needs. With this in mind, let's discuss "style"
for a minute. Every programmer has his or her own "style" or way he or
she likes to organize the logical flow of code. It makes sense to take advantage
of Perl5's object oriented, modular design whenever you write a Perl5 CGI. The power
and prominence of CGI.pm and other modules discussed in this book are a testament
to this. As you begin to conceptualize your Perl5 program, consider the following
questions:
<OL>
<LI>Is it modular? Major functions should be broken out into separate autonomous
modules that can be used in other programs.
<P>
<LI>Is it configurable? Configuration settings or variables should be located in
a separate file.
<P>
<LI>Is it understandable? Break your code down into subroutines that do specific
tasks. Major tasks that might be useful in other programs you write should be broken
out into modules. Program logic should consist of making calls to subroutines. Use
whitespace to make your code easier to read. Place comments where it is not obvious
what your code is doing.
<P>
<LI>Is it upgradeable? If you follow the first three directives, this one comes easy.
<P>
<LI>Is it portable? Avoid hard-coded references to resources only available on specific
hardware platforms. If this is not possible, specify them in a separate config file
instead of in the main code. Examples include pathnames and <TT>flock</TT> routines.
</OL>
<P>If you follow these five directives, you will save yourself considerable amounts
of time and grief, both as you write your code and as you try to maintain and upgrade
it in the future.</P>
<P>Listing 11.1 contains the main hypercal.cgi Perl5 CGI script. This script is responsible
for actually displaying the calendar itself.
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Listing 11.1. hypercal.cgi</FONT></H3>
<PRE><FONT COLOR="#0066FF">#!/usr/bin/perl
# HyperCal by Richard Bowen
# A HTML datebook.
#
# This part draws the calendar and links it to the other scripts.
# Can be called as http://URL/hypercal, or with arguments
# as http://URL/hypercal?month&year
require `httools.pl';
require `variables';
$args=$ENV{`QUERY_STRING'};
($sub=$ENV{`PATH_INFO'})=~ s#^/##;
if ($sub=~/personal/) { &personal };
if ($sub eq "goto") { &goto }
else { &main };
sub main {
&header;
($this_month,$this_year)=split(/&/,$args);
if ($args eq "") { # Defaults to current date if none specified.
&date; # Calls the todays date subroutine from httools.pl
$this_month=$month;
$this_year=$year;}
($junksec,$junkmin,$junkhour,$today_day,$today_month,$today_year,$junkwday,
Â$junkyday,$junkisdst)=localtime(time);$today_year+=1900;$today_month+=1;
&month_txt("$this_month");
print "<html><head><title>$title - $month_txt, $this_year</title></head>\n";
print "<body";
$month_image=@month_images[$this_month-1];
($icon,$bg,$color,$text,$link,$vlink)=split(/~~/,$month_image);
print " background=\"$bg\"" unless ($bg eq "none");
print " bgcolor=\"$color\"" unless ($color eq "none");
print " text=\"$text\"" unless ($text eq "none");
print " link=\"$link\"" unless ($link eq "none");
print " vlink=\"$vlink\"" unless ($vlink eq "none");
print ">\n";
open (DATES, $datebook);
@datebook=<DATES>;
@months=("December", "January", "February", "March", "April", "May", "June",
 "July", "August", "September", "October", "November", "December");
@last_days=(31,31,28,31,30,31,30,31,31,30,31,30,31);
@days_of_week=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
@month_offset=(3,3,0,3,2,3,2,3,3,2,3,2,3);
print "<center>";
print "<table border=6 cellpadding=5 width=100%>\n";
print "<tr><td align=center colspan=7><center><h2>@months[$this_month],
 $this_year</h2>";
if ($multi_user eq "yes" && $personal_on eq "no") {
print "<form method=post action=\"$base_url$hypercal/personal\">";
print "<input type=submit value=\"Go To Personal Calendar\">";}
elsif ($personal_on eq "yes") {
($link=$hypercal)=~s/\/personal//g;
print "<form method=post action=\"$base_url$link\">";
print "<input type=submit value=\"Go To Public Calendar\">";}
print "<img src=\"$icon\" alt=\"\" align=middle hspace=30>" unless
 ($icon eq "none");
print "</form>";
print "</center>";
$week_days=join(" <th> ",@days_of_week);
print "<tr><th>$week_days<br>\n";
for ($i=1906; $i<$this_year; $i++) {
$days_offset++;
if (($i)%4==0)
{$days_offset++}; # Leap years
} # end for
if (($this_year%4)==0 && ($this_month>2))
{$days_offset++}; # Current year is leap year
for ($j=1; $j<($this_month);$j++) {
$days_offset+=@month_offset[$j]};
$first_day_of_month=($days_offset%7);
$last_day_in_month=@last_days[$this_month];
if (($this_month==2)&&($this_year%4==0)){$last_day_in_month=29};
$date_place=0;
while ($date_place<$last_day_in_month) {
print "<tr>";
for ($j=0; $j<=6; $j++) {
if (($first_day_of_month>=0)||($date_place>=$last_day_in_month))
{print "<td align=center>-";
$first_day_of_month--}
else {
$date_place++;
print "<td align=center>
<a href=\"$base_url$disp_day?$this_month
Â&$date_place&$this_year\"> $date_place </a>";
&appoints($this_month,$date_place,$this_year);
if ($today_day==$date_place && $today_month==$this_month &&
Â$today_year==$this_year){print"<br><b><font color=red>TODAY</font></b>"};
} # end else
} #end for
print "<br>\n";
} # end while
open (ANNO, "$announce");
@announce=<ANNO>;
$search=$this_month."_".$this_year;
$any_announce="no";
for $announces (@announce) {
($mo,$msg,$aid)=split(/&&/,$announces);
if ($mo eq $search){
if ($any_announce eq "no"){print "<br><tr><td align=center colspan=7>";}
print "<center><b>$msg</b></center><br>";
$any_announce="yes";}
}
print "</table></center><br>\n";
print "<center>Select a day to see the appointments for that day.
Numbers in parentheses indicate how many appointments
are on that day.</center>";
print "<hr>\n";
print "<center>Go to:</center><br>\n";
print "<center>";
# print "<table border=0 cellpadding=5 width=100%>";
$last_year=$this_year;
$last_month=($this_month-1);
if ($last_month == 0) {$last_month=12; $last_year=($this_year-1);}
print "[ <a href=\"$base_url$hypercal?$last_month&$last_year\">
Previous month </a>|\n";
$next_year=$this_year;
$next_month=($this_month+1);
if ($next_month == 13) {$next_month=1; $next_year=($this_year+1);}
print "<a href=\"$base_url$hypercal?$next_month&$next_year\">
Next month </a>|";
print "<a href=\"$base_url$hypercal\"> Current month </a>]
</center><br>\n";
print "<center><form method=get action=$base_url$hypercal/goto>";
print "<input type=submit value=\"Jump\"> to <input name=\"month\"
size=2> \/ <input name=\"year\" size=4 value=\"$this_year\">";
print "<input type=hidden name=\"this_year\" value=\"$this_year\">";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -