Writing another chapter in chop()'s long epitaph, here's a patch to
perlport explaining how chop() is often used where chomp() should be.

Feel free to remove the Dunce::Files plug.

--- pod/perlport.pod    2001/02/03 09:59:28     1.1
+++ pod/perlport.pod    2001/02/03 10:03:17
@@ -94,6 +94,22 @@
 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(<FILE>) {
+        chop;
+        @array = split(/:/);
+        #...
+    }
+
+You can get away with this on Unix and MacOS (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 Dunce::Files module can
+help audit your code for misuses of chop().
+
 Because of the "text" mode translation, DOSish perls have limitations
 in using C<seek> and C<tell> on a file accessed in "text" mode.
 Stick to C<seek>-ing to locations you got from C<tell> (and no


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Cheating is often more efficient.
        - Seven of Nine

Reply via email to