in reply to Re^7: Error binmode() on unopened filehandle
in thread Error binmode() on unopened filehandle

    my $data = <<EOF;
    ...
    EOF

evidently deletes the <CR> characters.

As I understand it, in this particular case the CRs (carriage returns) are never there (in $data) to begin with. A here-doc is just another way to compose a string, in this case with double-quote interpolation (but that has no bearing here). Each line ends in a single  \n (newline) character.

Writing such a line to a Windoze "text"-mode (i.e., non-binmode-ed) file causes CRs to be added. This can be seen with an "ordinary" string containing newlines that is written in "text" mode and then read back binmode-ed:

c:\@Work\Perl\monks\Marshall>perl -wMstrict -e "use autodie; ;; use Data::Dump qw(dd); ;; my $s = qq{first\nsecond\n}; dd 's:', $s; print 'length: ', length $s, qq{\n}; ;; { open my $fh, '>', 'junque'; print $fh $s; close $fh; } ;; { open my $fh, '<', 'junque'; binmode $fh; my $t = do { local $/; <$fh>; }; dd 't:', $t; print 'length: ', length $t, qq{\n}; close $fh; } " ("s:", "first\nsecond\n") length: 13 ("t:", "first\r\nsecond\r\n") length: 15


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^9: Error binmode() on unopened filehandle
by Marshall (Canon) on May 07, 2020 at 03:53 UTC
    As I understand it, in this particular case the CRs (carriage returns) are never there (in $data) to begin with. A here-doc is just another way to compose a string, in this case with double-quote interpolation (but that has no bearing here). Each line ends in a single \n (newline) character.

    Not exactly right...The <CR>'s are there in the source Windows file but, the here-doc strips the <CR>'s out. I personally recommend using binary read() instead of the <> operator for actual files. I guess we are talking here about how to imbed binary data into a Perl source file?

    Note that on old Mac's, the text line ending is <CR> instead of <CR><LF> or <LF>.