Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.91k stars 542 forks source link

Heredocs silently change literal \r\n's and \r's to \n #17770

Open haukex opened 4 years ago

haukex commented 4 years ago
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Useqq=1;

print Dumper( eval           qq{"X\r\nY \r\n A \r B\n"}   or die $@ );
print Dumper( eval qq{<<"EOF"\r\nX\r\nY \r\n A \r B\nEOF} or die $@ );

Output:

$VAR1 = "X\r\nY \r\n A \r B\n";
$VAR1 = "X\nY \n A \n B\n";

I would have expected both outputs to be the same, that is, "X\r\nY \r\n A \r B\n". I've looked in the docs (perlop in particular), the Camel, and the issue tracker, but haven't found anything that documents this behavior.

Although it's possible I may have missed it in the docs, there are statements in the documentation that I find misleading, such as in the description of <<EOF in perlop: "Double quotes [on the terminator] indicate that the text will be interpolated using exactly the same rules as normal double quoted strings."

I think that the docs should be clarified to describe this behavior in a prominent place, and if someone could confirm that this behavior is intentional and not a bug, I could provide a doc patch. Otherwise, if this behavior isn't intentional, that would make this a real bug.

This behavior seems to be the same across all Perl releases since 5.6.2 (and probably before, I just don't have one installed to test on).

tonycoz commented 4 years ago

It looks intentional, originally added in f63a84b229f1dfb5c1bb698e1ed4bb362b3e6a6b.

Grinnz commented 4 years ago

A commit adding code gated with TMP_CRLF_PATCH with no explanation always inspires confidence...

If I had to guess, it's so that \r\n that commonly shows up in dos type files does not affect the contents of the heredocs.

xenu commented 4 years ago

Some ancient p5p threads about this topic: "[(pseudo) BUG] in here documents" - https://markmail.org/message/ihvj5jaumpiuxikc "CRs et al" - https://markmail.org/message/5goj3bcwhboqkcdg "[PATCH _71] CRs et al" - https://markmail.org/message/a52h64tm7a4laccp

tonycoz commented 4 years ago

From those links it appears to be deliberate.

haukex commented 4 years ago

Thanks for the replies everyone! I will look at the docs and I think I'll suggest a patch.