Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Printing with Here Documents

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.

Printing with Perl
Table of Contents
Using qq


Up to => Home / Authoring / Scripting / Tutorial




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers