Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

chomp() function and windows text format

by ozgurp (Beadle)
on Mar 24, 2010 at 21:26 UTC ( #830687=perlquestion: print w/replies, xml ) Need Help??

ozgurp has asked for the wisdom of the Perl Monks concerning the following question:

I have perl script that normaly works with text files saved as unix text file. However sometimes people use my scripts with text files saved in windows and they complain that it does not work. I checked the problem and noticed that chomp() function does not remove the end of line character from a windows text file. What is the best way of removing this character without having to convert windows text file to unix text file?
  • Comment on chomp() function and windows text format

Replies are listed 'Best First'.
Re: chomp() function and windows text format
by ikegami (Pope) on Mar 24, 2010 at 23:41 UTC
    chomp removes the contents of $/ from the end of the string (if present), and $/ defaults "\x0A" on all systems.

    Perl on Windows defaults to adding the :crlf PerlIO layer to handles. It causes CRLF to become LF on read, and the reverse on write. As such, when reading a Windows file on Windows, everything looks the same as it would on unix. If you read a line from a file containing "65 66 67 0D 0A", you'll get string "65 66 67 0A". chomp will happily remove the trailing line feed.

    When you'll have problems is when you read a Windows file on unix. The :crlf layer won't be present (by default), so the CR will appear in the read data along with the LF. chomp will happily remove (only) the trailing line feed, leaving the carriage return behind.

    Solution:

    while (<$fh>) { s/\s+\z//; # More flexible than chomp. ... }
Re: chomp() function and windows text format
by toolic (Bishop) on Mar 24, 2010 at 21:59 UTC
Re: chomp() function and windows text format
by ww (Archbishop) on Mar 24, 2010 at 21:58 UTC

    The statements throughout this question smack of misinformation or misunderstanding. You may want to read perldoc -f chomp (or, online, chomp).

    I think you'd also do well to show us some code and samples... and explain what the "scripts" are supposed to accomplish. Better, all the above and an explanation of their complaints (if anyone has provided detailed info; if not, do your own tests).
Re: chomp() function and windows text format
by cdarke (Prior) on Mar 25, 2010 at 06:48 UTC
    You might be using binmode on your file handle, in which case the '\r' removal is not done automagically.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2019-07-17 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?