Escape Sequences in Perl Statements that Print to a Browser

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. Escape sequences 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 sequences, or if I have made any mistakes herein, please send me an email, so that this page can be as accurate as possible.