Perl Programming/Advanced Output

From Wikibooks, open books for an open world
< Perl Programming
Jump to: navigation, search
Previous: User Input-Output Index Next: Filehandles

Advanced Output Overview[edit]

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[edit]

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.

# I said "I like mangos and bananas".
print '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:

# I said "They're the most delicious fruits".
print 'I said "They're the most delicious fruits".';

Escape characters[edit]

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.

 print 'I said "They\'re the most delicious fruits".';
 print "I said \"They\'re the most delicious fruits\".";

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:

 print " print \"I said \\\"They\\\'re the most delicious fruits\\\".\";"

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

Custom Quotes[edit]

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

 print qq{ I said "They're the most delicious fruits!". };
 print q! I said "They're the most delicious fruits\!". !;

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

Block Output[edit]

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.

 print <<OUTPUT
  I said "They're the most delicious fruits!".
 OUTPUT

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.

 print <<EverythingBetween
  ...
  ...
 EverythingBetween

Variable Output[edit]

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

my $one = 'mangoes';
 
print "I like $one.";    # I like mangoes.
print 'I like $one.';    # I like $one.
print qq@ I love $one.@; #  I love mangoes.
print q#I love $one.#;   # I love $one.
 
print <<OUT
 I love $one
OUT
;                        #  I love mangoes
 
print <<'OUT'
 I love $one
OUT
;                        #  I love $one

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:

my $one = 'lemon';
 
print "A $one is too sour; ";	# A lemon is too sour;
print "${one}ade is better.\n";	# lemonade is better.
 
print <<OUT
 I love ${one}s in $one souffle.
OUT
;                        #  I love lemons in lemon souffle.

Caveats[edit]

The single quote ' q{ and double quote " qq <<A 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.


Previous: User Input-Output Index Next: Filehandles