An alternative for Perl heredoc’s

Perl has (true to its motto – there more than one way to do it) many methods for declaring string. Here are a few:

  • The single quote (‘) – does not interpolate variables, does not understand escape sequences (like n for newline)
  • The double quote (“) – interpolates variables (replaces $foo with the value of the scalar foo), understands escape sequences
  • The generic single quote operator (can be written as q{}, q//, q() and so on – for details see the perlop page) – behaves just like the single quote, but uses an alternative separator character
  • The generic double quote operator (qq{}, qq//, qq(), etc) – the same for the double quote operator (interpolates variables)
  • The custom or heredoc (presumably an abbreviation for here comes a document) syntax

In general the best-practice is to choose an quotation operator based on two things:

  1. Do you need variable interpolation? If not, don’t choose an operator which interpolates them. This will make the script faster and communicate your intent
  2. Do you need to use characters in the string which are identical to the quotation operator itself and thus need to be escaped (like ” in “this is a “test””)? If so, you should choose an other quotation operator, the separator of which is not present in the script, thus no escaping (line-noise) is needed.

Finally, if you need to inject larger pieces of text (like HTML), you should use heredocs. Their general syntax is as follows (where separator can be an arbitrary word):

my $foo = $suff . <<SEPARATOR . $more_stuff;
some lines
and more lines

Some important things to remember about heredocs:

  • You need to use two less-than signs (not three as in PHP)
  • The separator is placeholder in the statement. So you need to finish your statement properly (with a 😉 before you begin the actual content of the strings. This also means that it needs not be at the very end of a line (as it is shown in most examples).
  • The final separator needs to be at the beginning of the line with no ; following it!

Finally, heredocs have also two variants: interpolating and non-interpolating. You can choose between the two versions by putting the initial separator between double quotes (for interpolating) or single quotes (for non-interpolating). By default, if you omit quotes, interpolation is used.

The biggest problem with heredocs is that they can’t be indented nicely, thus making a mess of your nicely formatted source code. String::TT comes to the rescue. The idea is really simple: take a multi-line string, get the shortest run of spaces preceeding a line and remove that many spaces from each line. It creates a much nicer look. If you have restrictions on installing modules from CPAN, you can take a peek at the source code and create your own version of it.

Leave a Reply

Your email address will not be published. Required fields are marked *