Front page | perl.perl5.porters |
Postings from July 2016
Re: Indented here docs?
Thread Previous
|
Thread Next
From:
wolfsage
Date:
July 1, 2016 01:00
Subject:
Re: Indented here docs?
Message ID:
CAJ0K8bgETJvMtUVRO-tA5j6GtXirDp=UXkj1y-r7+c741VHgPA@mail.gmail.com
I've now pushed a branch topic/indented-here-docs:
http://perl5.git.perl.org/perl.git/shortlog/refs/heads/topic/indented-here-docs
that implements the indented heredocs and removes leading spaces.
The commit is here:
http://perl5.git.perl.org/perl.git/commitdiff/ff554f6a9f6b0fdffbc2598f4bb17c2f818febf9
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:
<<~EOF;
<<~'EOF';
<<~"EOF";
<<~\EOF;
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
EOF
}
prints "Hello there\n";
The following:
if (1) {
print <<~EOF;
Hello There
EOF
warns and prints:
Indentation on line 1 of heredoc doesn't match delimiter at
/home/mhorsfall/eof.pl 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 :)).
Cheers,
-- 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