develooper Front page | perl.perl5.porters | Postings from October 2005

[perl #1807] chomp() can be confusing

From:
Steven Schubiger via RT
Date:
October 2, 2005 09:17
Subject:
[perl #1807] chomp() can be confusing
Message ID:
rt-3.0.11-1807-121923.5.11526920980039@perl.org
> [Ben_Tilly@trepp.com - Thu Nov 18 23:18:09 1999]:
> 
> Is there any possibility of having chomp() be modified to recognize \n, \r,
> and \r\n as line-endings to chomp?  A source of nasty confusion for people
> working in a cross-platform environment is when the same exact script gives
> very different results on the same exact file depending on whether you are
> running under *nix or Windows.  (Particularly an issue with Samba because
> people wind up reading under one system files created under the other.)

The record separator that is defaulted to '\n' on Unix, will change 
depending on the operating system Perl is running on - chomp() relies 
heavily upon the value of $/. Chomping native files shouldn't cause much 
noise, whereas chomping 'foreign' files with differing file-endings 
would require that you localize $/ in the scope of operation.

Example:
{
    local $/ = "\r\n";
    $chomped = chomp(@lines);
}

> Yes, the current behaviour works as documented.  But it leads to code not
> doing what people expect, and a confused person can easily spend several
> hours confused...

I'd say, it's rather clearly documented, without lack of accurate 
description. Although the behaviour requested is desirable, it doesn't
seem possible to integrate the inevitable changes to doop.c:Perl_do_chomp, 
where the record separator, known as global PL_rs, is extensively 
utilized and relied upon - allowing for multiple values would require 
tremendous changes and furthermore, would likely break backwards 
compatibility.





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About