gettime.pl
#!/usr/local/bin/perl5 -w
#
# gettime.pl
#
# The #!.../perl lets the operating system know what program
# to use to process this program. This type of statement
# is also used to start up shell scripts.
#
# Any line starting with a "#" indicates that the line is a
# comment. In general, the "#" can be used to mark the rest
# of the line as a comment as well.
#
# Get the time out of server machine.
#
# This is a quick hack given that the network time protocol
# exists. However, it does work, and is reasonably effective.
#
###
### Configuration
###
# I tend to put configuration information towards the start
# of my programs. This means that I only need to look in one
# place when I need to change those items that I realized
# beforehand were configuration issues.
#
# Variables in Perl are prefixed by special characters.
# The "$" character indicates that the variable is a "scalar"
# variable. It will contain a single value. That value
# can be numeric or a string.
#
# All statements in Perl must be terminated with a
# semicolon.
$host = 'kira.peak.org';
###
### Query the time server
###
### Many Internet machines have a time service. We will be
### using this service.
###
# The localtime function (which is described in the book)
# returns an array. This next statement calls the localtime
# function, selects the 8th entry (the timezone), and stores
# it in a variable.
$dst = (localtime())[8];
# Like "C", Perl has a ternary operator. If the expression
# is True, the result is the expression after the "?",
# otherwise the result is the expression after the ":".
#
# You will find all of the "C" operators in Perl. You will
# also find many of the operators from shell programming
# as well. Perl treats strings as a data type, and there
# are a number of operators involving strings as well.
$tz = ($dst) ? "PDT" : "PST";
# The "system" function sends the command in quotes to the
# operating system to be executed, much as the "C" function
# of the same name.
system("date > /dev/null"); # Load the date pgm into the cache
# The backquotes (watch the direction carefully) also sends
# a command to the system. This syntax comes from shell
# programming (and you can see that Perl is an interesting
# combination of "C", shell programming, and later you'll
# see awk, sed, and tr.)
#
# This command starts up telnet to talk to the specified host
# on port 13, which simply returns the time as a text string
# along with information about telnet starting up and shutting
# down. The pipe through grep looks for the line containing
# the two colons from the time, so we get only the line
# containing the time.
#
# $_ is a special variable. Many functions (or commands)
# will operate on this variable if no other variable is
# specified. The "chomp" deletes the last character of the
# line, which is the newline character.
#
# The grep command uses a "regular expression". It says
# look for two colons separated by exactly two characters,
# whose contents we don't care about. While this regular
# expression is for the grep command, you will find that
# they play an important part of Perl. The set of regular
# expressions is more fancy than most other Unix utilities
# use, so pay attention to that sections of whatever book
# you use.
$_ = `/usr/net/bin/telnet $host 13 | grep ":..:"`;
chomp;
###
### Set the local time
### Avoid doing things which take time between getting the time
### from kira and setting the time locally.
###
# The "if" statement can be a little different from most
# computer languages (although a little closer to human
# language). The "length" is just checking the number of
# characters in the time string to make sure that we received
# something which could possibly be a time. If not, we exit.
exit if length($_) < 5;
# Strings in Perl are a bunch of character enclosed in quote.
# marks. However, variables are expanded in the string.
# So the "$_" in the string printed below gets expanded into
# the string which the time server sent back. It doesn't
# matter if the variable is a string variable or a numeric
# variable; it gets expanded into a string as you
# normally want.
print "Time from kira is $_\n";
$oldtime = time;
print "New time is: ";
# The backslashes before the quote marks indicate that these
# characters don't terminate the string, but are literal
# characters which in this case are sent to the system.
system("date -s \"$_ $tz\"");
$newtime = time;
if ($oldtime < $newtime)
{
print "Time has been advanced by ",
$newtime - $oldtime, " seconds\n";
}
# Note that Perl uses an "elsif" statement instead of
# "else if" as would be used in "C". Also, blocks of
# statements must be enclosed in a pair of braces.
elsif ($oldtime > $newtime)
{
print "Time has been retarded by ",
$oldtime - $newtime, " seconds\n";
}