=head1 TITLE Heredoc contents =head1 VERSION Maintainer: Richard Proctor Date: 27 Aug 2000 Last Modified: 1 Oct 2000 Mailing List: perl6-language@perl.org Number: 162 Version: 2 Status: Frozen =head1 ABSTRACT The content of a Heredoc is normally included into the program verbatim. RFC 111 allows whitespace (and comments) on the terminator. This RFC covers the content. It introduces the <<< Enhanced Heredoc that removes whitespace and discusses the provision of other dequoting options in the library and documentation enhacements that should follow. =head1 DESCRIPTION =head2 Preamble I originally wanted to remove leading whitespace from the lines in a heredoc, several other people wanted to remove whitespace equivalent to the shortest span of whitespace at the start of lines, or the whitespace from the first line. TomC pointed out ways to achieve the removal of the whitespace in current perl, although this sort of works (as long as the user is consistent about use of spaces and tabs). I would like to make life easier. This attempts to bring all these ideas together. =head2 Discussion of options There are several possible ways that have been discussed: a) No Indenting - this is the current behaviour of <<. b) Remove all leading whitespace from all lines of input. This was not popular - no longer supported in this RFC. c) Remove whitespace equivalent to the first line of the Heredoc This was not popular - it did not fit many peoples requirements. d) Remove whitespace equialent to the smallest whitespace - a Realistic option, this can be performed by using regexes and the dequote function. e) Remove whitespace equialent to the terminator - a realistic option. This takes the whitespace off the content equivalent to that on the terminator and removes that amount of whitespace from the content. (This is now proposed for <<<). f) Using a Heredoc and a regex to remove unwanted whitespace. TomC provided some examples showing how this would work, and howw this could handle many of the options above. g) Using a Heredoc and a function to handle the dequoting of the content. This is essentially the same as a regex, but allows common types of dequoting to be written once. =head2 Agreements There are three things that have been agreed:- =head3 Enhanced Heredoc There will be two types of heredocs, the simple < function handles all these cases. It expects to be called with a here document as its argument. It looks to see whether each line begins with a common substring, and if so, strips that off. Otherwise, it takes the amount of leading white space found on the first line and removes that much off each subsequent line. sub dequote { local $_ = shift; my ($white, $leader); # common white space and common leading string if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) { ($white, $leader) = ($2, quotemeta($1)); } else { ($white, $leader) = (/^(\s+)/, ''); } s/^\s*?$leader(?:$white)?//gm; return $_; } =head2 example s/// tricks print <<"EOF" =~ /^\s*\| ?(.*\n)/g; | Attention criminal slacker, we have yet | to receive payment for our legal services. | | Love and kisses | EOF print <