Read an Excerpt
PREFACE: Intended Audience
Are you a programmer? Do you understand scripting and shell languages? Do you know Unix? If you can answer yes to these three questions, then the book shouldn't be hard for you.
This text is a bit Unix-centric, but since Tcl and Perl both run on non-Unix platforms most of the code herein is portable. And the book does contain code. This book was intended to be used by programmers who have an understanding of systems analysis and design. It would also be good if they have some Tcl or Perl experience. While Tcl and/or Perl experience isn't required, a programming language is, and if you don't know Tcl or Perl, it is recommended that you look at obtaining one of the texts listed in Appendix B on either or both of the languages.
So, if you want to learn about CGI programming, and you want to learn why things are done and not just how things are done, then this book will most likely interest you. If you are looking to learn Tcl or Perl from this text, you came to the wrong place.
Reason for Being Here
This book was written after I had a chance to see what was available on the market for people that wanted to really understand how to make the World Wide Web work for them in real-world terms.
I had just completed my first article (what has grown into the code in Chapter 5) on a forms to E-mail CGI for Mecklermedia's Web Developer Magazine (initial issue) and I had handed in my second (on firewall selection) when I was asked if I would be interested in writing a book on CGI development. The problem was, I didn't want to write a book that would become another one of those cute things thatpeople buy, but never use. I wanted to do something different.
I was lucky to have been found by the folks at Prentice Hall PTR, who were willing to give a totally untested author a chance to do more than turn out yet-another-CGI-book. They allowed me to explain what I wanted and to then follow that vision.
The core reason for this entire book was not to create one you read and tossed away. I wanted it to be more that that. I wanted it to be a teaching tool, a tool that provided a way for you, the reader, to get more out of it than to just solve a problem one way in one language.
A great deal of that has to do with my frustration on how most books are written. Too many times people seem to forget to put the why they did something next to the how they did it. This leads to the knowledge that a single problem an be solved with a single method. For instance, Chapter 12 takes things taught up to that point and shows you how to use those techniques for another purpose.
In today's parlance you could say this is about code reuse. But it really isn't. A person once told me that there were only seven basic stories in the world, and that all other stories derived from these seven basic as the roots. I wanted to try to show that there are millions of different problems that can be solved with CGIs in the environment of the Web, but that all of these problems have common roots and once you understand that, they can all be solved.
I also wanted to avoid the flame wars that happen when you get language-centric. In Chapter 4 the book goes into greater detail on this, but to get it into it's most simple reason, it's because CGI coding isn't about languages, it's about solving problems.
Let's take Chapter 5 as an example. Why did I create a Tcl-based program to do a forms to E-mail CGI? It all started when I wanted to do more than just dump out the information in a form to E-mail, I wanted to manipulate it. So I took a look at the C program that had been written by Thomas Boutelle and decided that to make some changes. Being a terrible C programmer (my background being VAX Fortran and Macro) I decided to look at other alternatives. At this same time I had just been exposed to Tcl, so I took 20 minutes to generate email.tcl V0.5.
Now, there weren't that many differences between the C program and the Tcl one, but it was working. And it had the added functionality that I had wanted. Then one day someone asked for a program to generate E-mail from a form on the Net, and I gave them mine. They pointed out that I had some serious errors in mine (all having to do with unescaping the passed-in data) and suggested that I look at using uncgi.c (then at version 1.11). I did and rewrote the code. That created version 1.0-the first production version. The version in this book should be called 3.0 as it has continued to grow over time. Not only did it grow from my needs and those of my employers, but from those who used it from the Net and those that created other forms to E-mail programs that had functionality that I thought would make this program even more useful. Now, why duplicate it in Perl? Why not use Matt Wright's very popular version? Because we wanted to show that the language isn't the issue, the application is. That is what this book is all about-the application.
Who is I?
In 95 percent of all cases, an I in this book is J.M. Ivler. The other 5 percent is Kamran Husain. It should be noted that this book wouldn't be as good as it is had I (jmi) not had Kamran here to help. You see I have this problem, my Tcl looks like Fortran, and my Perl looks like bad Tcl. As a Tcl programmer by preference, I knew that I wouldn't be able to do justice to the Perl code if I were to attempt to write both. I really needed someone who could write Perl, and use the features of Perl, not someone who wrote Perl that didn't make use of some of it's key strengths.
I was fortunate enough to find a co-author who was willing to take the requirements for each program in each chapter and generate matching Perl software. The book is designed to allow you to unplug a Tcl routine and plug the Perl one in it's place, and have it work. Kamran was the person who made that happen. So while I may be I in 95 percent of the book, the book wouldn't have been possible without the us that Kamran made it.
I'm going to start by sharing how I originally envisioned the chapters and then provide a chapter by chapter breakout so you can see what really happened.
Chapter 1: What is CGI Code?
Chapter 2: C, Tcl, Perl, Shell - Language Overview
Chapter 3: UNCGI - Why it is used.
Chapter 4: Basic Form Processing
Chapter 5: Form to E-mail
Chapter 6: Query the files - Building a search processor
Chapter 7: Building a CGI based page counter
Chapter 8: Query a database - Report forms
Chapter 9: Guest books and Comments
Chapter 10: Closing Comments
Well, here is what we actually ended up delivering:
1 Introductory information on the technology behind the Internet
2 A more in-depth look at the technology and the methods of the Internet, a sharper focus on Web-based issues like URLs and URIs and the statelessness of transactions
3 A look at CGIs and the HTTP protocols including a look at server side includes and state with cookies.
4 Unencoding and working with the data of the transaction. A quick review on the strengths of the languages that we choose for the book
5 Program: Form dumping to E-mail or the screen
6 Program: Counters on the Net, and an introduction to the log files
7 Program: Redirection from one location to another and an introduction to logging
8 Program: Randomness and randomness with direction
9 Program: A calendar utility that is very flexible
10 Program: Writing and maintaining data files
11 Program: Server Side Includes (complex directory list)
12 Program: Using what we learned to build a commercial application
13 Program: Intranet (An office directory and more)
14 Program: Intranet (Systems management on a Web server)
15 Server Side Includes (.htaccess and server side security)
16 HTTPd server setup and log files
17 Closing thoughts (and "What's New")
As you can see, this is much different from what I was going to write originally. Yet, in some strange way, still very much the same.
We (in the following instances is Kamran and me) also had to make some choices. One key choice was how data was to be handled. This was one of those lowest common denominator issues. I really wanted to do a chapter showing how to manipulate a database, but what database was I to use? Oracle? Informix? Sybase? In order to make sure that this could be used by anyone, we decided to forgo all the above. We went with flat data files instead of a database. We figured that if you could do a join in a flat data file, you should have no problem doing it in an SQL statement on a database.
The tenet of lowest common denominator became a basic principle of this text. The only area where that became a problem was when we got to platforms. In that case we chose to make sure that the code worked on a Unix platform first, and then tried to ensure that it ported to non-Unix platforms. In most cases the software port did work. When it didn't we usually stated so in the chapter by the area that isn't portable.
Yes, we have included a compact disk with this book. However, we didn't include all those things that you normally find on a CD. We stuck with the things talked about in the book, the software we created, and some Perl and Tcl distributions.
The CD has been mounted and read on an Apple, PCs under MS Windows 95 and Windows NT 4.0 as well as Linux and BSD. In addition, we mounted and read the CD on an HP's HP-UX, Sun's Sun-OS and Solaris, IBM's AIX and Digital's AXP. Our guess is that it should be mountable and readable on your system.
Tcl runs under most Unix operating systems, as does Perl. In addition, Tcl distributions are available for PCs and Macs. If you need a C compiler to compile either of the source distributions, we recommend that GNU versions of the ANSII C compiler as acceptable. As an overview, the CD contains 26 Perl programs, 27 Tcl programs, 3 image libraries (of digits 0-9), some smattering of HTML and some data files (that were used in the chapters). You'll also find a C program for unencoding passed in data, and a number of Tcl and Perl distributions.
In some cases there may be a need to modify a few of the book's programs to run on different platforms. In general we have attempted to keep the code as portable and platform-independent as possible. There are a small number of routines that do have Unix dependencies, but these are pointed out in the comments in the code, and suggestions for work-arounds are provided in the code comments.
A full list of the contents of the disk, as well as some additional information about the CD can be found in Appendix C. In addition, an exclusive Web site for this book (www.prenhall.com/developers_ resource_series) will be kept up to date with the latest information on porting issues in the code and pointing to the latest core distributions.
Conventions Used in this Book
To avoid confusion, we have tried to distinguish the different items that appear throughout the book. For example, all in-text names of specific variables, forms, options, commands, file names, etc. appear in helvetica font. Italics are used to emphasize key words and terms.
It's also crucial to articulate people's roles precisely in this book. For example, in the heavily coded chapters 5-14, we the authors sometimes take on the role of an application developer or a system designer, while you the reader are sometimes an application engineer who has to deal with various groups within his organization. Hopefully, we haven't muddied the waters.
We also had to make a hard choice on how the book would be read. Fortunately, code written in Tcl and Perl does not require lines to be less than 80 characters long. Unfortunately, the book's production specs do. This created a debate between the authors, who wanted to keep lines intact and make the book landscape, and the editors, who wanted the lines broken up and the book to be portrait. As you can see, the editors won. In the code we took care of long lines by using standard language continuation marks. In the data files and other locations where the line extended over the character limit for the page, we have indicated that the line is continued by placing a Þ at the beginning of the continued line.
I tend to think in lists, and even then I know that I'll leave someone out, I hope I don't. To those that made this possible, my co-author, Kamran Husain, Acquisitions Editor Mary Franz, Development Editor Jim Markham (even if he is a Pat's fan), and Technical Reviewer Cameron Laird (one of those people who makes me better than I am). I also want to thank David Fiedler of Mecklermedia for agreeing to publish me, way back when.
To Thomas Boutelle, Tom Christiansen, Matt Kruse, Matt Wright and all the rest of the people who make comp.infosystems.www. authoring.cgi the newsgroup that it is. To John Ousterhout for Tcl, and Larry Wall for Perl. To the crew at Sunsoft for keeping Tcl free, and to the folks who populate comp.lang.tcl for making this community ever that much better through their efforts to share everything.
To Information Management Associates, Inc. and Daniel Riscalla, Vice President of Research and Development, for their time and patience and for being the alpha test site. To all my co-workers for their constant support and harassment, YES! It is finally completed and no, I couldn't have done it without you. You constantly raised the bar, thanks.
To Mr. Sloan who told me "Just because you're learning disabled doesn't mean you have to prove you're stupid!" And to my brother Robert who said "Find something you like doing and then find someone foolish enough to pay you to do it." I'm not stupid, and I'm getting paid!
To Hattie Little, Lynn Foggle, Myrtle Mallenger, Hannes Callisen, Dan Van Ostrand, Brian McEvoy and Barbara and Hillel Disraelly, you always knew. To Steve, Sunny and Nicholas, thanks for the time (We have no plans for the weekend, come on over). Lastly, to my three graces. My mother Barbara; was it worth the nine months yet? My wife Thuy, "Honey, I'm home! Now get the heck out of my kitchen." And to my darling angel. one-and-a-half-year-old Brooke, "Now, daddy can play." A more supportive threesome I could never had asked for. Thank you all.