views:

82

answers:

2

I have a Perl Script which was built on a Linux platform using Perl 5.8 . However now I am trying to run the Perl Script on a Windows platform command prompt with the same Perl version.

I am using this command perl rgex.pl however it gives me one whole chunk of errors which looks to me like it has already been resolved in the script itself. The weird thing is I am able to run another Perl script without problem consisting of simple functions such as print, input etc.

The Code:

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor;

my $file = "C:\Documents and Settings\Desktop\logfiles.log";
open LOG, $file or die "The file $file has the error of:\n =>  $!";

my @lines = <LOG>;
close (LOG);

my $varchar = 0;

foreach my $line ( @lines ) {
if ( $line =~ m/PLLog/ ) 
{
    print("\n\n\n");
my $coloredText = colored($varchar, 'bold underline red');
print colored ("POS :: $coloredText\n\n", 'bold underline red');
$varchar ++;        
}
print( $line );
}

When I run on the windows command prompt it gives me errors such as:

  • Unrecognized escape \D passed through at rgex.pl line 7.
  • => No such file or directory at rgex.pl line 8.

Please give some advice on the codes please. Thanks.

+7  A: 

A \ in a Perl string enclosed in double quotes marks the beginning of an escape sequence like \n for newline, \t for tab. Since you want \ to be treated literally you need to escape \ like \\ as:

my $file = "C:\\Documents and Settings\\Desktop\\logfiles.log";

Since you are not interpolating any variables in the string it's better to use single quotes:

my $file = 'C:\Documents and Settings\Desktop\logfiles.log';

(Inside single quotes, \ is not special unless the next character is a backslash or single quote.)

codaddict
Works beautifully! Thanks mate!
JavaNoob
It is better to use single quotes in this case as no variable interpolation is required.
Alan Haggai Alavi
@Alan: Added it to the answer. Thanks.
codaddict
+2  A: 

These error messages are pretty clear. They tell you exactly which lines the problems are on (unlike some error messages which tell you the line where Perl first though "Hey, wait a minute!").

When you run into these sorts of problems, reduce the program to just the problematic lines and start working on them. Start with the first errors first, since they often cascade to the errors that you see later.

When you want to check the value that you get, print it to ensure it is what you think it is:

 my $file = "C:\\D....";
 print "file is [$file]\n";

This would have shown you very quickly that there was a problem with $file, and once you know where the problem is, you're most of the way to solving it.

This is just basic debugging technique.

Also, you're missing quite a bit of the basics, so going through a good Perl tutorial will help you immensely. There are several listed in perlfaq2 or perlbook. Many of the problems that you're having are things that Learning Perl deals with in the first couple of chapters.

brian d foy
I went through the tutorials but none of them covered Perl regular expressions used in Windows.
JavaNoob
Well, there's no regular expression problem here, and this isn't a Windows issue. It's just a single-quoted string. We cover this in _Learning Perl_, so maybe you need a better tutorial. :)
brian d foy