[tpm] chomp problem

Stuart Watt stuart at morungos.com
Fri Feb 4 14:59:16 PST 2011


This is puzzling. For me, the script behaves exactly as it ought, even 
on Windows. Even if I change the script to use UNIX line terminators, it 
behaves as it ought. And (see following script) even when I add in a 
binmode() call to turn off Perl/Windows automatic conversion of \cM\cJ 
to \n, it still behaves just as predicted.

I may be missing something, but it looks like there may be some oddity 
in the DATA file handle. I copied the data to a separate file, and 
modified the script, leaving in the binmode. Now, the same data is in 
the DATA file handle, and in both cases I used the Windows line 
terminations. Now, if I check the byte-level output (with "perl test.pl 
| od -c"), I get different behaviour with versus without the open call, 
which ought to read the same data.

In effect, binmode(DATA, ':raw') has no effect on the inline DATA block. 
This suggests maybe there are some unusual effects specific to the DATA 
filehandle. This wouldn't account for what you see, but certainly I'd 
test it reading from a regular filehandle for comparison. What 
version/build of Perl are you using?

All the best
Stuart

# Modified script to play with DATA filehandles
use warnings;
use strict;

open(DATA, "<", "data.txt"); # comment out this line, to compare with it 
included

#binmode(\*DATA, ':raw');    # turn off \cM\cJ => \n on read, has no 
effect without open() call

while (<DATA>) {
         chomp $_;
         print "$_";
}

__END__
a
b
c
d
e
f
g
h


On 04/02/2011 3:42 PM, Omid Gulban wrote:
> Hello All,
>
> I have a simple script shown below:
> on windows xp platform.
>
...
> Why do I get an output that looks like this:
> a
> ab
> bc
> cd
> de
> ef
> fg
> ghh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20110204/31b63f2b/attachment-0001.html>


More information about the toronto-pm mailing list