Object-Oriented CGI Support
HTML is not much fun to write by hand, and writing program code to produce it via print statements can be even less fun. Mistakes in the code might go unnoticed if you test on one browser and not others; malformed code might be interpreted charitably by one browser but cause another to crash.
The CGI class brings some order into the potential chaos of HTML generation by tying the page code into the structure of a Ruby program. One immediate benefit is that it enforces correct nesting of tags, so that mistakes like <center><em>this</center></em> can't happen. Here are two equivalent CGI scripts. The first, in Listing 20.9, is written the brute-force way:
Listing 20.9. cgi-bin/cgi-test.rb
01: #!/usr/local/bin/ruby 02: ... |
Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.