grep ^M in file using perl script....

hi
i am using perl on windows ( active state perl 5.8 ) and i want to check for Control-M (^M) in file. files to be checked are in unix format so new line character is (\n). This perl script is called from Batch file ( windows .BAT file )
my script is

          while (<PROGRAM>) {
                  chomp $_ ;
                  if ( /^M/ ) {  # ^M - control-v + control-M
	         $useError = $1 ;
                      $checkFail++ ;
                      printf STDOUT "LineNo %6d : [$_] - \'Cntl+M\' used.\n", $. ;
                  }
             }
             close PROGRAM ;

this script is not working because when i do chomp perl removes ^M ( as its a new line character in windows ) so how can i check for ^M on windows perl ? ( it works if ^M is in between line )

reason i am doing this on windows is that this BATCH file is executed when somebody check in file into PVCS ( which is on windows server ) as all files are for unix servers they should not have ^M and i cant use SFU perl if i do that then i have to make sure that everyone using PVCS should have SFU installed on their system. so you must be wondering who puts ^M in this case some times people opens the file in notepad and then saves the file.

you can remove them in unix using dos2unix

sed -e 's/^M//g' file > newfile

^M must me untered with CTRL+V, CTRL+M

i dont want to remove ^M what i want is when user runs this script with 1 input parameter as file to be checked for ^M it should tell if file hash ^M or not ..

like

$ ./check.pl test.lst
LineNo     42 : [       xxxxxxxxxxxxxxxxx xxxxxxx ^M] - 'Cntl+M' used.
1 Lines needs to be changed.

and i can not use Unix commands here not even sed,awk i only have perl ( windows version ) installed. this script is wrapped in windows BATCH file.

#!/usr/bin/perl
while (<>) {
   print if $_ =~ /\cM\n/g;;
}

# if you want to remove it
#while (<>) {
 #  print if $_ =~ s/\cM\n/\n/g;;
#}

hey thanx for the reply this code works perfectly on UNIX but when i execute it on windows it does not work. in both the cases same file was used.

even the code i have written works ...but not on windows ..

in windows, it should not be ^M. you can try \r\n . otherwise, i am out of ideas

thanx ghostdog74 for your reply ...

here is the file i am trying to check for presense of ^M. following is the output produced on unix server

$cat -v texx
hi ^M
what are you
tying to ^M
do
$
$te.pl texx
hi i am
tying to
$
$cat te.pl
#! /usr/bin/perl
while (<>) {
       print if $_ =~ /\cM\n/g;;
}
$

is there any other way of doing this other than using reg-ex ?

this does not work to see if ^M is in the lines on Windows?

while (<>) {
   print "Line number $. has ^M in it\n" if ($_ =~ /\cM/);
}

If not, maybe there is no ^M in the lines.

i am putting ^M to test my code but it doesnt work on windows i can check for presence of ^M in file on unix but when i transfer the same file on windows with the same perl code ( but perl here is windows version of perl ) it doesnt work .
i have tried following ways to filnd ^M ...( all works on unix but not on windows )

if ($_ =~ /\cM/) ;
if ($_ =~ /\015/) ;
if ($_ =~ /\x0D/) ;
if ( $_ =~ /^M/ ) ;

lets see if its possible by any other way .... i will post the answer it if i am able to solve this

make sure when you try to simulate ^M,, you don't press Shift 6 then "M". press Ctrl-V then M.

how about: /\r/ to find ^M on windows?

tried almost everything but nothing seems to work here.

can i check same thing in C ?? i know only basics of C so if can give me some suggesting to check for ^M or ( 015 in octal , 0d in HEX ).

hey i found solution to this by opening file in Binary mode and then looking for '0d' in hex

@line_array = () ;
open (FILE,"<$ARGV[0]") or die "cant open file :$! \n" ;
while (<FILE>) {
     binmode(FILE) ;
     $line1 = unpack("H*", $_) ;
     #print "[$line1]\n" ;
     if ( $line1 =~ /0d/ )
     {
	push @line_array , $. ; 
     }
}
close FILE ;
print "file has \'cntl-M\' please check\n" if ( ( $len = @line_array ) >= 1 ) ;