http://qs321.pair.com?node_id=11110973


in reply to Print Behavior with Carriage Return

Note that you can open a file with CRLF line endings for reading with the :crlf IO layer. chomp will then remove both line ending characters.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MData::Dumper -E + ' my $fileStr = qq{val1=key1\r\nval2=key2\r\nval3=key3\r\n}; open my $inFH, q{<:crlf}, \ $fileStr or die $!; my %hash; while ( <$inFH> ) { chomp; my( $v, $k ) = split m{=}; $hash{ $k } = $v; } close $inFH or die $!; print Data::Dumper ->new( [ \ %hash ], [ qw{ *hash } ] ) ->Sortkeys( 1 ) ->Dumpxs();' %hash = ( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3' );

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Print Behavior with Carriage Return
by haukex (Archbishop) on Jan 05, 2020 at 18:33 UTC
    chomp will then remove both line ending characters.

    Nitpick: I/O layers don't change the behavior of chomp, it will continue to remove $/, which is typically one character, "\n", even on Windows. The I/O layer converts \r\n to \n when reading, so the strings end in just "\n" before they are even touched by chomp.