Escape Sequences in Perl Statements that Print to a Browser

Share this page with your friends

Inadvertently putting a metacharacter in the wrong place when writing a Perl script can lead to disastrous results. This tutorial will discuss the use of escape sequences to avoid this problem. Perl escape characters are used for other purposes as well. These will also be considered, but I will not go into their use in regular expressions, as this is another topic altogether.

While searching for references to this topic, I discovered many contradictory statements about what characters need to be escaped and the effects of escape sequences. I have tested everything that follows using a Unix server in both Internet Explorer and Firefox. Perhaps things work differently in other environments, but what follows should be of use if you are writing a Perl script to be used in these popular browsers.


Handling metacharacters:

The metacharacters in the table will cause problems unless precautions are taken. There are two ways to stay out of trouble.
Metacharacters
the dollar sign     $

the ampersand       @

the backslash       \

the double quote    "

1) If the metacharacter is enclosed in single quotes, it will be treated as a literal character, and need not be escaped.

     print '$1.69';
                              # Displays as:   $1.69
     $price = '$1.00';
     print $price;
                              # Displays as:   $1.00

2) If you must use double quotes to contain a string, then use the escape character   (backslash   \  )   in front of any metacharacter.

print "The price is \$13.95 each."; # Displays as: The price is $13.95 each.

NOT handling metacharacters:

What happens if you don't handle the metacharacters?

1) When you try to use a string that contains a $ or an @, Perl thinks that you are specifying a variable name or an array name. Everything is omitted after the metacharacter (until a character appears which is not legal for use in a variable name).

print "The price is $13.95 each."; # Displays as: The price is .95 each.

In this statement, Perl thinks that $13 is a variable name, so it prints nothing, since it doesn't have a value for that variable. The next character after the 3 is a decimal point. This is an illegal character for a variable name, so Perl thinks that the variable name has ended, and the print statement resumes with that character.

2) If you have a backslash in front of a non-metacharacter, the character will print normally (unless the -w warning switch is being used in the shebang statement, in which case a warning will be generated).

     print "Hell\o";
                          # Displays as:   Hello

3) Not escaping a double-quote will cause a syntax error. ( See the Frequently-made mistakes in Perl tutorial. )


Other escape sequences:

The escape sequences in the table were found to work when printing to a browser, but offhand I can't think of any useful application for them.
Escape Sequences (work as advertised)
\l    The next letter is lower case
        print "HELP \lME NOW";       # Displays as:   HELP mE NOW

\u    The next letter is upper case
        print "help \ume now";       # Displays as:   help Me now

\L    Changes the following letters to lower case (until an \E is reached)
        print "HELP \LME\E NOW";     # Displays as:   HELP me NOW

\U    Changes the following letters to upper case (until an \E is reached)
        print "help \Ume\E now";    # Displays as:   help ME now

\E    Ends the \L or \U sequence

In the final table are listed the escape sequences which don't work as advertised in print statements that are output to a browser, but may be useful in other situations.

Escape Sequences (do something else)
\a    Alarm (beep)

\b    Backspace

\cX   Control Character (like CTRL-X)

\e    Escape Character

\f    Form Feed

\n    New Line

\Q    Quote meta-characters as literals

\r    Carriage Return

\t    Tab Character

These sequences are effective when printing to a file, but to properly format string output in a browser, you must use HTML coding. For example, using a New Line character ( \n ) will move to the next line when you are printing to a file, but to do this in a browser, you need to use <br />.

In a browser window, Alarm, Backspace, Control, and Escape sequences print a little square . Feed, Newline, Return, and Tab print a space. The Quote sequence is very erratic. It prints at least one backslash, but not immediately after the escape sequence!

If anybody has more information than I was able to find about these escape characters, or if I have made any mistakes herein, please send me an email, so that this page can be as accurate as possible.


Comments

There are currently no comments to display.

Click here to add a comment

Rate this page:






Share this page with your friends


Tutorial Menu

  • Frequently-made
    Mistakes in Perl
  • SEO: Image
    Used as a Link
  • Escape Sequences
    in Perl
  • 11 Checkpoints
    for Accessibility
  • Cloaking Affiliate
    Links

RSS Feeds

  • Add 'Professors Coding Corner' to my feed reader.
  • Preview Professors Coding Corner on Feedage
  • Please preview this RSS feed on FeedAg, and rate it for me.
  • Professor's Ezine Articles about Web Programming

Everything you need to know about building your blog.

Professor, 7807 Lerkenlund, St. Thomas, VI 00802 Tel: 340.715.3542
Copyright ©  Professional Website Design Website by: The Professor external link About Me Accessibility Legal Site Map
Professional Website Design logo

Professor's Coding Corner

  • Home
  • Code Snippets
  • Tutorials
  • Resources
    • HTML & CSS
    • Javascript
    • Perl
    • Tools
    • Other
  • Articles
  • Contact