Perl Programming/Advanced output

Advanced output overview
In many situations, especially for web programming, you will find that you want to put certain things, such as backslashes or quotes, in your text that aren't allowed in a traditional print statements. A statement such as

print "I said "I like mangos and bananas". ";

will not work because the interpreter would think that the quotes mark the end of the string. As with all things in Perl, there are many solutions to this problem.

Use other quotes
The quickest solution to this problem would be to use single quotes to surround the string, allowing the use of double quotes in the middle.

print 'I said "I like mangos and bananas".';
 * 1) I said "I like mangos and bananas".

This is obviously not the best solution, as it is conceivable that you are trying to print a string containing both kinds of quote:

print 'I said "They're the most delicious fruits".';
 * 1) I said "They're the most delicious fruits".

Escape characters
For situations like the above where only a short amount of text is being quoted, a common solution is to escape any quotes in the string. By preceding any quotes with a backslash they are treated as literal characters.

Using single quotes, the characters that require escaping are \'.

Using double quotes, the characters that need escaping are the variable sigils, (i.e. $@%*) in addition to \"

Using \ to escape reserved characters of course implies that you also need to escape any backslashes you want to use in your string. To print the second line literally using perl, you would need to write:

Luckily, Perl provides us with another way of quoting strings that avoids this problem.

Custom quotes
Perl provides the operators q and qq that allows you to decide that characters are used to quote strings. Most punctuation characters can be used. Here are a few examples:

The only symbols I have found that cannot be used for these quotes are $ ` /

Block output
As can be seen, while the custom quotes option works for short strings, it can run into problems, if a lot of text containing a lot of punctuation is output. For this situation, a technique called Block quoting can be used.

Any string of characters can be used instead of OUTPUT in the example above. Using this technique anything can be output no matter what characters it contains. The one caveat of this method is that the closing OUTPUT must be the first character on the line, there cannot be any space before it.

Variable output
It is possible to output variables within strings when you use some of these methods:

Perl will figure out where your variable ends, if the character after it is neither a letter, number nor an underscore. If that is not your case, put your variable inside curly braces:

Caveats
The single quote  ' q{  and double quote <tt> " qq <<A </tt> operators, behave differently. Whereas when using double quotes, you can include variables and escape any characters, when you use single quotes you can only escape single quotes and you cannot include variables.