Due by 10:30 p.m. on Tuesday, February 2.
A. Reading
In the text, read the comments on style on pages 546-550, skipping any that refer to things you haven't had yet. It's time to start keeping style in mind in writing Perl scripts. We don't have to follow the style suggestions religiously, but when we deviate we should have in mind a reason why.
B. Problems to do but not to submit
/usr/dict/words
and files in the h
directory.
public_html
directory with no contents except a little text
(e.g., Have a nice day
), name the file, say, little.html
,
make it readable to all, and try looking at it with your
browser. Does your browser complain that HTML tags are missing,
or does it just show it? If you make your text in two paragraphs,
separated by a blank line, what happens?
C. Problems to submit
make_perl
that will help get you going
on any Perl project by making a ``skeleton'' script for you to
start from. The usage will be
make_perl scriptname
where you can put any name you want
for the script name.
This script should check to make sure there isn't already a file
by that name; open a file with the given name, for writing into;
put the proper heading information in the file; and make the
file executable by you, or if the name ends in .cgi
, make the
file executable by all.
To do this, you'll need some Perl commands that will be discussed
in section Thursday or lecture Friday, although you could also
read up on them earlier: -e
, open
, print FILEHANDLE
,
and chmod
or `...`
or system(...)
.
index.html
and display it if one is found. Make a personal home page by
putting a file with that name in your public_html
directory
and making it readable by all. Its HTML tags should be based on
HTML ideas that will be discussed in section and in class. Do
what you want with it.
However, it is not necessarily a good idea to put personal information on the Web. Also, this page will disappear after the end of the course. Therefore if you wish it can be an imaginary web page, with made-up information--but if you do that it might be good to label it as imagined.
No need to submit this one, since it can be viewed directly.
A simple database is usually organized into ``records'', each with ``fields''. An example is the roster database used in Problem 2.3, where each record is on one line and the fields are separated by colons. You wrote a script to take the roster database and print it out with each field on a separate line, preceded by a label giving the field name. Records were separated by blank lines.
We treated this second form as just a report, but it's also a
nice way to enter data in the first place, if it's up to you to
make the original roster file. The only change you might want
to make in the format is that instead of full field names, such
as Name: Makam, Huli
, you can use something shorter such as
N: Makam, Huli
. (One letter is enough since in this database
the field names each start with a different letter.) Also,
it's not necessary to line up the information vertically. After
typing the data this way, you can use a Perl script to put the
data into the original database form with colon separators.
Write a script that does this. (As before, ignore the last two fields in the roster file; just don't make them.) In other words, the input to the script should be the same as your output in Problem 2.3 except for using one-letter field names and not lining up vertically; the output should be the same as the input in Problem 2.3 except for omitting the last two fields.
Some considerations:
(no information)
and use a substitution to change that to being empty.
push
to
build up the array.
/^[A-Z]\:/
and then take
all the rest of the line. (It's necessary to ``escape'' special
characters, i.e., non-alphanumeric characters such as
()[]{}|
, with \
when you are want them for themselves, for
example \(
, rather than using them as controls. A colon
doesn't happen to be a control now, but it might be in a future
version of Perl, so play it safe.) One way to get the rest of
the line is to add a parenthesized part to your pattern: /^[A-Z]\:(.*)/
and then refer to $1
, whose contents will be
the part found inside the parentheses.
s/...//
where the part to substitute is
empty. (What patterns are used?)
defined
that
tells whether a value is defined. In checking to see that all
values are defined, your script might have something like
unless (defined $data{$field})
or maybe
defined $data{$field} || die ...
join
function to get the output line.
undef
function. Otherwise there is a danger
that a missing field in one record might simply be replaced by
the same data in the previous record--a very bad error!
Here just some ingredients have been listed. You should think through the needed logic and plan the program yourself, but you can consult with the TA and instructor as needed.