Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: [SOLVED]: Problem when using chomp on line read from file.

by johngg (Canon)
on Feb 26, 2018 at 23:01 UTC ( #1210002=note: print w/replies, xml ) Need Help??


in reply to [SOLVED]: Problem when using chomp on line read from file.

I see that you have resolved your problem while I was composing this response but I might as well post it anyway in case it is helpful.

As you found, chomping MS Windows line terminators on a *nix type system without taking extra measures causes problems. Using split, ord, sprintf and map can be helpful in visualising what is happening to your data, it is much clearer than just printing each line. Your solution is to modify the default line terminator, which is the second item in the following code. Note the difference pre-chomp between that method and the :crlf I/O layer in my third item which actually removes the carriage return during the readline leaving just the line feed.

use strict; use warnings; use feature qw{ say }; my $winLineTerm = qq{\x0d\x0a}; my( $file, $writeFH, $readFH ); say q{-} x 20; open $writeFH, q{>}, \ $file or die $!; print $writeFH qq{ABC${winLineTerm}DEF$winLineTerm}; close $writeFH or die $!; say q{No measures}; open $readFH, q{<}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; say q{-} x 20; say q{Change default line terminator}; { local $/ = $winLineTerm; open $readFH, q{<}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; } say q{-} x 20; say q{open() with :crlf I/O layer}; open $readFH, q{<:crlf}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; say q{-} x 20;

The output.

-------------------- No measures Before chomp() 0x41 0x42 0x43 0xd 0xa After chomp() 0x41 0x42 0x43 0xd Before chomp() 0x44 0x45 0x46 0xd 0xa After chomp() 0x44 0x45 0x46 0xd -------------------- Change default line terminator Before chomp() 0x41 0x42 0x43 0xd 0xa After chomp() 0x41 0x42 0x43 Before chomp() 0x44 0x45 0x46 0xd 0xa After chomp() 0x44 0x45 0x46 -------------------- open() with :crlf I/O layer Before chomp() 0x41 0x42 0x43 0xa After chomp() 0x41 0x42 0x43 Before chomp() 0x44 0x45 0x46 0xa After chomp() 0x44 0x45 0x46 --------------------

I hope this is of interest.

Cheers,

JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210002]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2018-09-26 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (205 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!