eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

introduction to web programming
Printing with Here Documents  
Unfortunately, much of what your CGI applications will be sending to the Web browser will include the double-quote mark. It becomes tedious, especially for long blocks of HTML code, to make print statements for every line of HTML and to escape every occurrence of a double-quote with a backslash. Consider the following table definition:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<TABLE BORDER = \"1\" CELLPADDING =
\"2\" CELLSPACING = \"2\">";
print "<TR>";
print "<TD ALIGN = \"center\">Email</TD>";
print "<TD ALIGN = \"center\"> <A HREF =
\"mailto:selena\@foobar.com\">selena\@foobar.com</A></TD>";

print "</TR>";
print "</TABLE>";

If any one of those backslashes are missing, the whole script breaks down. And this is a very small block of code!

One solution to the sending of large blocks of HTML code which incorporate the double-quote is to use the "here document" method of printing. The "here document" method tells Perl to print everything within a certain block of boundaried code. The "here document" uses the generic format:

    print <<[TEXT_BOUNDARY_MARKER];
    [Text to be printed]
    [TEXT_BOUNDARY_MARKER]

For example, this code will print out the basic HTML header:

    #!/usr/local/bin/perl
    print <<end_of_html_header;
    Content-type: text/html\n\n
    <HTML>
    <HEAD>
    <TITLE>My Title</TITLE>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    end_of_html_header

In short, the "here document" method of printing tells the Perl interpreter to print out everything it sees (print <<) from the print line until it finds the text boundary marker specified in the print line (end_of_html_header). The text boundary marker can be anything you like of course, but it is useful to make the flag descriptive.

Further, the ending flag must be "exactly" the same as the flag definition. Thus, the following code will fail because the final flag is not indented correctly:

    print <<"    end_of_header";
    <HTML><HEAD>
    <TITLE>$title</TITLE>
    </HEAD><BODY>
    </BODY></HTML>
    end_of_header

The final "end_of_header" tag should have been indented four spaces, but it was not indented at all.

Previous | Next | Table of Contents