Strange problem with newline (\r\n) under Win32 
Author Message
 Strange problem with newline (\r\n) under Win32

Hi,

I've a perl script with the following line:

1      my $response = <STDIN>;
2      chomp $response;
3      if ($response eq 'y') {
      ...

It works. But after I make a copy of this script and make some change, it
doesn't works. The chomp $response; onle remove the \n but not the \r which
make the if failed. I've debug both scripts, after line 1, both scripts has
the same $response = "y\r\n"; but after line 2, the original script has the
$response = "y" but the new has the $responese = "n". I've check the system
var $/ the are same = "\n". Anybody have an idea what going wrong?

Br/Jasom



Thu, 19 May 2005 17:46:15 GMT  
 Strange problem with newline (\r\n) under Win32
Hello


Quote:
> 1      my $response = <STDIN>;
> 2      chomp $response;
> 3      if ($response eq 'y') {

> chomp ... onle remove the \n but not the \r which
> make the if failed. ... I've check the system var $/ the are same = "\n".

A:
The following will protect you against infamous \n-vs-\r\n differences:

      my $response = <STDIN>;
      chomp $response; $response =~ s/\r$//;
      if ($response =~ /^[Yy]/ ) {

, which will work for both textmode and binmode filehandle.
(Setting $/ to "\r?\n" is tempting but won't help .. $/ is not a regex).

Yakov Lerner



Fri, 20 May 2005 02:58:46 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. win32 newline problem

2. Strange problem installing Win32-OLE-0.1403 module (PLEASE HELP )

3. Strange Problem with Win32::Console

4. Strange problem: strange character added during a mkdir

5. newline problem with messy output - v simple problem

6. Replacing multiple occurences of newline chars with a single newline char

7. STRANGE PROBLEM: Filename problem

8. newline problem with formatted write

9. format picture fields with newlines problem

10. Newline problem

11. Problem checking for newline in $_ input line

12. newlines and query -> param / problem with apostrophes

 

 
Powered by phpBB® Forum Software