From: Xavier Noria Date: Tue, 2 May 2006 12:08:34 +0000 (+0200) Subject: [DOC PATCH] chop warning in perlport X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=31bb23b166683ca82ab1aa9af0910e80eb18a1d3;p=p5sagit%2Fp5-mst-13.2.git [DOC PATCH] chop warning in perlport Message-Id: p4raw-id: //depot/perl@28054 --- diff --git a/pod/perlport.pod b/pod/perlport.pod index de753e7..b639c47 100644 --- a/pod/perlport.pod +++ b/pod/perlport.pod @@ -94,21 +94,9 @@ from) C<\015\012>, depending on whether you're reading or writing. Unix does the same thing on ttys in canonical mode. C<\015\012> is commonly referred to as CRLF. -A common cause of unportable programs is the misuse of chop() to trim -newlines: - - # XXX UNPORTABLE! - while() { - chop; - @array = split(/:/); - #... - } - -You can get away with this on Unix and Mac OS (they have a single -character end-of-line), but the same program will break under DOSish -perls because you're only chop()ing half the end-of-line. Instead, -chomp() should be used to trim newlines. The L module -can help audit your code for misuses of chop(). +To trim trailing newlines from text lines use chomp(). With default +settings that function looks for a trailing C<\n> character and thus +trims in a portable way. When dealing with binary files (or text files in binary mode) be sure to explicitly set $/ to the appropriate value for your file format