Search code examples
perlcpanmultilinecode-formattingliterals

Unindenting multiline string literals


In Perl it is possible to write multiline string literals using ordinary quotes or here-doc statements, but because code indents are counted as the content of the string itself, this either ruins formatting of surrounding code, either requires us to additionally unindent string before using. At this point I use the following subroutine to unindent string literal:

sub unindent {
    local $_ = shift;
    $_ =~ s/^\s*$//mg;
    $_ =~ s/^(\s*>>>\s)//mg;
    return $_;
}

print unindent "
    >>> #pragma once
    >>> #include <...>
    >>> ";

But not to re-invent the wheel, I'd like to ask if there is already something for this purpose built-in into Perl or on CPAN?


Solution

  • After some more googling I've found some packages with similar features and partially solving the problem:

    The first option requires lots of dependencies to be installed (for simple unindent function), the rest three are for here-docs only. None of these packages seem to support additional markers (>>> in the question) which to my mind would be safer than plain indents (due to tab-space mixture problems).