Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Why chomp() is not considering carriage-return

by dse (Novice)
on Apr 30, 2018 at 16:22 UTC ( #1213854=note: print w/replies, xml ) Need Help??


in reply to Why chomp() is not considering carriage-return

I do a lot of work in Windows (Cygwin), macOS, and Linux, and I have a Subversion repository containing a bunch of dotfiles I use on all three platforms. As a result I do, in fact, wind up getting a nice healthy mix of CRLF and LF line endings in all environments I work in. Because of that, $/ = "\r\n"; won't work.

Also, I can confirm that chomp in Cygwin perl will only remove \n from lines in text files with CRLF line endings ($/ is \n by default).

I just now learned about \R which *will* do the trick:

s{\R\z}{};

As an added bonus it will also work with old Macintosh line endings (\r)!

I honestly don't know why chomp doesn't do this in the year of our lord two thousand eighteen, other than backward combatibility.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2019-07-17 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?