home -- outline -- lectures -- assignments -- discussion -- tips -- links --



CGI & Perl (cont.)

When we store information in a file, one of the easiest ways to "delimit" it is with carriage returns. Given then Perl tends to read files a line at a time (where a line ends at a CR), it is especially easy to use this technique.

All we have to do is add carriage returns to the end of data chunks when we are printing to a file, and then read each chunk back in as we get ready to present it. This is what we did in the script that records people's names to a file. The first script grabs the name and puts it in a file. The display script then steps through the file line by line printing each in turn.




But what happens when the information we are gathering is not singular?

For example, what if we really do want the first name and the last name of a visitor. We can print the information to one line, but how do we make sure we can reconstruct it?

Well, first we have to add the means to gather the information to the form that we are using.
What is your first name?
What is your last name?



This is all well good, but what have we got in our file now? We seem to be saving only the first name so when we look at the file, that is all we see.

One way to fix this is to print both the first and last names before we print the CR.




What is your first name?
What is your last name?



Now we have both the first and last name going into the file, but we there is still a problem.




We could add in a space to the print statement, but this doesn't really help us. It still is the case that what was two pieces of information (the first name and the last name) has now become one piece of information. What we need to do is preserve the distinctions between these pieces of data.

One thing can do is to generalize the same approach that we use when we add carriage returns. We can further "delimit" the information using character other than just CR. For example, we could use the space that we were thinking of putting in as a way of dividing the peices of data. In effect, we are saying:

Everything before the SPACE is one element and everything after is another.


This seems like an okay idea, but first we have to put the space in...

What is your first name?
What is your last name?



Then when we build the script to read back and print the data, we are going to have to change it so that it splits the data using the fact that the space is there.