Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: chomp() function and windows text format

by ikegami (Pope)
on Mar 24, 2010 at 23:41 UTC ( #830702=note: print w/replies, xml ) Need Help??


in reply to chomp() function and windows text format

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. ... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2019-07-18 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?