in reply to How to remove a carriage return (\r\n)

Being a paranoid programmer myself, I always use:
sub remove_trailing_newline { $_[0] =~ s/[\r\n]+\Z//; }
no matter which platform I am on. A real trick is trying to find blank lines in a slab of (multiline) text:
if ( m/(\r\n|\n\r|\r|\n)$1/ ) { # two newlines in a row! }

Update: Thanks to rev_1318 for pointing out that the $1 need be replaced with \1 in the regexp.

Replies are listed 'Best First'.
Re^2: How to remove a carriage return (\r\n)
by rev_1318 (Chaplain) on Nov 02, 2005 at 12:45 UTC
    if ( m/(\r\n|\n\r|\r|\n)$1/ ) {
    You mean
    if ( m/(\r\n|\n\r|\r|\n)\1/ ) {
    Backreferences inside the RE are notated as \1, \2, etc.

    Paul