develooper Front page | perl.perl5.porters | Postings from July 2016

Re: Indented here docs?

Thread Previous | Thread Next
July 1, 2016 01:00
Re: Indented here docs?
Message ID:
I've now pushed a branch topic/indented-here-docs:

that implements the indented heredocs and removes leading spaces.

The commit is here:

  Allow indented heredocs.

  This adds a new modifier '~' to heredocs that tells the parser
  that it should look for /^\s*$DELIM\n/ as the closing delimiter.

  These syntaxes are all supported:


  The '~' modifier will strip, from each line in the heredoc, the
  same whitespace that appears before the delimiter.

  Newlines will be copied as is, and lines that don't include the
  proper beginning whitespace will throw warnings.

  Some examples:

    if (1) {
      print <<~EOF;
      Hello there

  prints "Hello there\n";

  The following:

    if (1) {
      print <<~EOF;
     Hello There

  warns and prints:

    Indentation on line 1 of heredoc doesn't match delimiter at
/home/mhorsfall/ line 6.
     Hello there

My C is rusty so some extra eyes would be wonderful.

I haven't tested with PERL_STRICT_CR, and there's other logic inside
of scan_heredoc that I haven't figured out what it's for yet (starting
with the lines (in toke.c):

     /* inside a string eval or quote-like operator */
    if (!infile || PL_lex_inwhat) {

I'd love to see this make it into blead (or something like it).

Other implementations try to be smart about tabs and spaces, but it
seems to me just making sure all lines in the heredoc match the
delimiter in terms of identical indentation, it should be fine...
(it's at least a hell of a lot easier :)).


-- Matthew Horsfall (alh)

P.S. (porting/diag.t will fail currently in my branch because I
haven't added the new warning to perldiag...)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About