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

[perl #1807] chomp() can be confusing

Steven Schubiger via RT
October 2, 2005 09:17
[perl #1807] chomp() can be confusing
Message ID:
> [ - 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.

    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. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About