April 07, 2004 (Wednesday)

Miscellaneous Perl

Using the null pattern during split

We can use the null pattern // to split a scalar into an array in which each element represents a character of the scalar. Therefore, the expression:

split //, "hello"

will return an array containing the elements:

('h', 'e', 'l', 'l', 'o')

The localtime function

The localtime function will determine the current local time. It returns an array consisting of the various attributes related to the current time including the time and date, the current week day, day of the year and whether or not daylight savings time is in effect. (See S&P p.164 for details.)

We can also provide localtime with a timestamp and it will return the appropriate time/date information for that timestamp. A timestamp is simply the number of seconds that have elapsed since the so-called epoch, which, for most UNIX systems, is the beginning of 1970.

If we evaluate the localtime function in scalar context (instead of array context), we will get a human readable form of the date. The following Perl script demonstrates some uses of the localtime function:

#!/usr/bin/perl -w

use strict;

my @lt = localtime;

# @lt = ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
print "@lt\n";

print "The current time is ", scalar localtime, "\n";

print scalar localtime 2**31-1, "\n";
print scalar localtime 2**31, "\n";

The above script displays:

23 44 17 6 3 104 2 96 1
The current time is Tue Apr  6 17:44:23 2004
Mon Jan 18 23:44:07 2038
Fri Dec 13 17:15:00 1901

A timestamp, when stored in a signed 32-bit value, can hold the number of seconds from 1970 to early 2038 only. After that, the signed timestamp becomes negative meaning that it will be interpreted as a date in late 1901. Any machines (especially embedded devices) that use signed 32-bit values in 2038 will experience rollover problems.

Array slices

Consider the case where a programmer wishes to determine the current year, month and date. The following script works correctly, but is inelegant:

#!/usr/bin/perl -w

use strict;

my @time = localtime;

my $year = $time[5] + 1900;
my $month = $time[4] + 1;
my $day = $time[3];

printf "%4d-%02d-%02d\n", $year, $month, $day;

The year stored in the sixth element of the array returned by localtime denotes the number of years since 1900, therefore, we must add 1900 to the year value in order to get a proper four digit year. We add one to the month since the month returned by localtime represents January as 0.

We can make the above script a bit more compact by doing a multiple assignment:

#!/usr/bin/perl -w

use strict;

my @time = localtime;

my ($year, $month, $day) = ($time[5] + 1900, $time[4] + 1, $time[3]);
printf "%4d-%02d-%02d\n", $year, $month, $day;

The assignment is still a bit repetitious. Perl provides a feature known as array slices which allows one to extract a subset of the elements of an array, as demonstrated by the following example:

#!/usr/bin/perl -w

use strict;

my @time = localtime;

my ($year, $month, $day) = @time[5,4,3];
printf "%4d-%02d-%02d\n", $year+1900, $month+1, $day;

Note that we write @time[5,4,3] and not $time[5,4,3], because the result of an array slice is another array, not a scalar. Also note that we must remember to add 1900 and 1 to the year and month respectively during our output.

Comments on the Final Exam

The final exam will cover the entire course (C, C++ and Perl). However, because the midterm was almost entirely C, the number of questions on the final related to C will be relatively small. The emphasis of the exam will be on C++ and Perl. There may also be a question or two related to all three languages, so you should be familiar with the similarities/differences and strengths/weaknesses of each language relative to one another.

The structure of the final exam will likely be similar to the midterm exam, but there will be more questions (maybe eight?). There may even be a few multiple choice questions. You should review all the assignments and their solutions and make sure you understand them. Even though Assignment #9 was optional, you are still responsible for the material covered by that assignment. (Incidentally, if you find any mistakes in the solutions to any of the assignments, please let me know.)

Material for which you are responsible

Questions from the final exam will be based upon:

The online notes as well as sections in K&R that are referenced by the online notes.
Chapters 0 up to and including Chapter 13 of K&M. A few concepts from Chapter 15 (pure virtual functions, abstract base classes, forward declarations) are also fair game. You are also responsible for material summarized in the online notes and discussed in detail in the text.
Chapters 1 up to and including 15 of S&P as well as the online notes. The online notes give further elaboration and more examples of the topics discussed in the above chapters. We did talk briefly about array slices and non-greedy quantifiers as well. These are mentioned in Chapter 17.
Again, make sure that you understand the assignments and their solutions. Anything covered in class (even if it isn't in the online notes or in any of the textbooks) could be asked on the final.

Instructor Availability and Other Miscellaneous Stuff

Good luck on your finals!

Last modified: April 7, 2004 14:37:47 NDT (Wednesday)