NAME
Filter::HereDocIndent - Indent here documents
SYNOPSIS
use Filter::HereDocIndent;
# an indented block with an indented here doc
if ($sometest) {
print <<'(MYDOC)';
Melody
Starflower
Miko
(MYDOC)
}
outputs (with text beginning at start of line):
Melody
Starflower
Miko
HereDocIndent mimics the planned behavior of here documents in Perl 6.
INSTALLATION
Filter::HereDocIndent can be installed with the usual routine:
perl Makefile.PL
make
make test
make install
DEPENDENCIES
HereDocIndent requires Filter::Util::Call, which is part of the
standard distribution starting with Perl 5.6.0. For earlier versions of
Perl you will need to install Filter::Util::Call, which requires either
a C compiler or a pre-compiled binary.
DESCRIPTION
HereDocIndent allows you to indent your here documents along with the
rest of the code. The contents of the here doc and the ending delimiter
itself may be indented with any amount of whitespace. Each line of
content will have the leading whitespace stripped off up to the amount
of whitespace that the closing delimiter is indented. Only whitespace
is stripped off the beginning of the line, never any other characters
For example, in the following code the closing delimiter is indented
eight spaces:
if ($sometest) {
print <<'(MYDOC)';
Melody
Starflower
Miko
(MYDOC)
}
All of the content lines in the example will have the leading eight
whitespace characters removed, thereby outputting the content at the
beginning of the line:
Melody
Starflower
Miko
If a line is indented more than the closing delimiter, it will be
indented by the extra amount in the results. For example, this code (+
is used to indicate spaces):
if ($sometest) {
++++++++print <<'(MYDOC)';
++++++++Melody
+++++++++++Starflower
++++++++Miko
++++++++(MYDOC)
}
produces this output:
Melody
+++Starflower
Miko
HereDocIndent does not distinguish between different types of
whitespace. If you indent the closing delimiter with a single tab, and
the contents eight spaces, each line of content will lose just one
space character. The best practice is to be consistent in how you
indent, using just tabs or just spaces.
HereDocIndent will only remove leading whitespace. If one of the lines
of content is not indented, the non-whitespace characters will not be
removed. The trailing newline is never removed.
INDENT_CONTENT
By default the contents of the here document are indented to the same
extent as the closing delimiter. If you want to leave the contents
indented, but still indent the closing delimiter so that it lines up
with its content, set the INDENT_CONTENT option to zero in when you
load HereDocIndent:
use Filter::HereDocIndent INDENT_CONTENT=>0;
NWS
BUG: Please note that there is a bug I haven't resolved with NWS
filtering. If the {nws} string appears at the beginning or end of the
heredoc then it's not stripped out. In the middle it should be OK.
The NWS option helps you clean up the contents of heredocs by allowing
you to add whitespace in your perl code but have it stripped out when
your program runs.
To enable NWS ("no whitespace") filtering, add the NWS option to the
"use" command:
use Filter::HereDocIndent NWS=>1;
Anywhere in a heredoc that HereDocIndent sees the string {nws} it will
strip out that string and all surrounding whitespace. NWS is handy for
outputting strings like HTML where avoiding whitespace can clutter up
your code. For example, the following code will output HTML without any
spaces between the tags:
print <<"(HTML)";
{nws}
{nws}
(HTML)
LIMITATIONS
HereDocIndent was written to be conservative in what it decides are
here documents. HereDocIndent recognizes the most common usage for here
docs and disregards other less common usages. If you constrain your
here doc declarations to the format recognized by HereDocIndent (which
is by far the most popular format) then your code will compile just
fine.
The format recognized by HereDocIndent is a single print statement or
variable assignment, followed by <<, then a quoted string or unquoted
string of word characters, then a semicolon, then the end of line. Here
are a few examples that would be parsed properly by HereDocIndent:
print << '(MYDOC)';
print << "MYDOC";
my $var = <