Main

April 02, 2004 (Friday)

File tests and Directory operations (S&P — Chapters 11 and 12)

The script below, when run on one of the Computer Science machines will generate a file in the current directory named course.html which contains all the 3710 online course notes for the current semester in a single HTML file. You should note the following regarding the generated file:

Despite the above shortcomings, the script demonstrates many new features in Perl that we have not seen yet. In particular, it demonstrates the use of file tests, some directory operations, extracting user information from the password file, file globbing, and introduces how we run and store the output of external system commands (e.g. wget) in Perl. (This last topic is expanded further in subsequent chapters.)

#!/usr/bin/perl -w

use strict;

my $output = "course.html";
die "File '$output' exists!" if -e $output;
open OUT, "> $output" or die "Cannot open '$output': $!\n";

defined(my $dir = (getpwnam "donald")[7]) or die "No such user!";

$dir .= "/.www/comp3710/diary/";

undef $/;
open STYLE, "$dir/styles.css" or die "Cannot find style file: $!";
my $style = <STYLE>;
close STYLE;

print OUT <<"END";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<title>Course Notes &mdash; Computer Science 3710 (Winter 2004)</title>
	<style type="text/css">
$style
	</style>
	</head>
<body>
END

$dir .= "2004/";
die "No such directory '$dir'" if ! -d $dir;

chdir $dir or die "Cannot change to '$dir': $!\n";

for (glob "*/*/index.php") {
	if (! -r) {
		print "File $_ is not readable.";
		last;
	} elsif (! -s) {
		print "File $_ is empty.";
		next;
	}
	my $url = "http://www.cs.mun.ca/~donald/comp3710/diary/2004/$_";
	print "Getting $url...\n";
	my $result = `/usr/bin/wget -O- $url 2>/dev/null`;
	die "Not able to retrieve web page!" unless $result;
	my ($html) = ($result =~ m{<body>(.*)</body>}s);
	print OUT $html;
	print OUT "<hr>\n"
}
print OUT "</body>\n</html>";
close OUT;
notes.pl

Once you've generated the web page, you can load it in into a web browser such as firefox or konqueror and ``print'' it to a PostScript file. (This may take a minute or two.) Then, you can use the psnup utility to convert the PostScript file into another PostScript file that has two pages per sheet:

$ psnup -2 -pletter course.ps > course-2up.ps
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] 
[18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31] [32] 
[33] [34] [35] [36] [37] [38] [39] [40] [41] [42] [43] [44] [45] [46] [47] 
[48] [49] [50] [51] [52] [53] [54] [55] [56] [57] [58] [59] [60] [61] [62] 
[63] [64] [65] [66] [67] [68] [69] [70] [71] [72] [73] [74] [75] [76] [77] 
[78] [79] [80] [81] [82] [83] [84] [85] [86] [87] [88] [89] [90] [91] [92] 
[93] Wrote 93 pages, 5128846 bytes
$

The resulting course-2up.ps file can then be printed.


Last modified: April 7, 2004 15:43:00 NDT (Wednesday)