Perl advanced output overview

Perl advanced output overview

Perl advanced output overview 150 150 Roderick Derks

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.

# 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

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\".";

This of course implies that you also need to escape any backslashes you want to use in your string. To print the line I have written above literally in 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

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

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

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

Collect data and print it in a nice way on the screen

# this is to create a report that can be printed to the screen or to a file
formline q{@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<< @<<<<<<<<<<   @>\@<<
}, $mac, $ip, $dns, ${StackTypeNumber}, ${stack_copy}, ${switch}, ${port};
# this is to print the collected statistics to a variable
$rapport = $^A;

This is an internal function used by formats, though you may call it, too. It formats (see the perlform manpage) a list of values according to the contents of PICTURE, placing the output into the format output accumulator, $^A (or $ACCUMULATOR in English). Eventually, when a write() is done, the contents of $^A are written to some filehandle, but you could also read $^A yourself and then set $^A back to “”. Note that a format typically does one formline() per line of form, but the formline() function itself doesn’t care how many newlines are embedded in the PICTURE. This means that the ~ and ~~ tokens will treat the entire PICTURE as a single line. You may therefore need to use multiple formlines to implement a single record format, just like the format compiler.

Be careful if you put double quotes around the picture, because an “@” character may be taken to mean the beginning of an array name. formline() always returns TRUE. See the perlform manpage for other examples.

 

Caveats

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.

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your Name (required)

Your Email (required)

Subject

Your Message

Your Name (required)

Your Email (required)

Subject

Your Message