logo

internet.com
Join the free
WDVL newsletter!

home

Printing with Perl

Using the print function

  • The most basic method for sending text to the Web browser is the "print" function in Perl. The print function uses the following syntax:

    print "[string to print]"; 
    

  • By default, the print function outputs data to standard output "<STDOUT>" which in the case of a CGI application, is the Web browser. Thus, whatever you tell Perl to print will be sent to the Web browser to be displayed.

  • For example, the following CGI script sends the phrase, "Hello Cyberspace" to the Web browser:

    #!/usr/local/bin/perl
    print "Content-type: text/html\n\n"; 
    print "Hello Cyberspace"; 
    

  • However, print does have some limitations. For example, the print function is limited in its ability to handle Perl special characters within an output string. For example, suppose we want to print the HTML code:

    <A HREF =
    "mailto:selena@foobar.com">selena@foobar.com</A>
    

  • You might extrapolate from the syntax above, that you would use the following Perl code to display the hyperlink:

    #!/usr/local/bin/perl
    print "Content-type: text/html\n\n"; 
    print "<A HREF =
    "mailto:selena@foobar.com">selena@foobar.com</A>";
    

  • Unfortunately, this would yield a syntax error. Additionally, because this is a very common line of HTML, it is a common source of Perl CGI customization errors. The problem lies in the incorporation of the at sign (@) and double-quote (") characters within the code.

  • As it so happens, these characters are "special" Perl characters. In other words, they each have special meaning to Perl and, when displaying them, you must take precautions so that Perl understands what you are asking for.

  • For example, consider the double quote marks in the "mailto" hyperlink. How would Perl know that the double quote marks in the "mailto" hyperlink are supposed to be part of the string to be printed and not actually the end of the string to be printed? Recall that we use the double quote marks to delineate the beginning and the ending of a text string to be printed. Similarly, the at sign (@) is used by Perl to name list arrays.

Many other "special" characters exist and are discussed in other Perl references.

  • One solution to this problem is to escape the Perl special characters with a backslash (\). The backslash character tells Perl that whatever character follows should be considered a part of the string and not a special character. Thus, the correct syntax for the mailto hyperlink would be

    #!/usr/local/bin/perl
    print "Content-type: text/html\n\n"; 
    print "<A HREF =
    \"mailto:selena\@foobar.com\">
    selena\@foobar.com</A>";
    

Printing with Perl
Table of Contents
Printing with Here Documents

Up to => Home / Authoring / Scripting / Tutorial

Instantly look up a "techie" word using Webopedia!!


internet.com
e-commerce
WebDeveloper Network


Autonomy - The Future of Information Management

Copyright 1999 internet.com Corporation. Legal Notices. Privacy Policy.

www.internet.com