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

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.


while (<$fh>) { s/\s+\z//; # More flexible than chomp. ... }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://830702]
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
Find Nodes?
    Voting Booth?

    No recent polls found