我有一个 Perl 脚本,它是使用 Perl 5.8 在 Linux 平台上构建的。但是现在我正尝试在具有相同 Perl 版本的 Windows 平台命令提示符下运行 Perl 脚本。
我正在使用这个命令 perl rgex.pl
但是它给了我一大堆错误,在我看来它已经在脚本本身中解决了。奇怪的是我能够毫无问题地运行另一个 Perl 脚本,该脚本由打印、输入等简单功能组成。
代码:
#!/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 );
}
当我在 Windows 命令提示符下运行时,会出现如下错误:
- 无法识别的转义\D 在 rgex.pl 第 7 行通过。
- => rgex.pl 第 8 行没有这样的文件或目录。
请提供一些关于代码的建议。谢谢。
最佳答案
用双引号括起来的 Perl 字符串中的 \
标记转义序列的开始,例如 \n
表示换行,\t
表示标签。由于您希望 \
按字面意思对待,因此您需要像 \\
一样将 \
转义为:
my $file = "C:\\Documents and Settings\\Desktop\\logfiles.log";
由于您没有在字符串中插入任何变量,因此最好使用单引号:
my $file = 'C:\Documents and Settings\Desktop\logfiles.log';
(在单引号内,\
并不特殊,除非下一个字符是反斜杠或单引号。)
关于windows - 为什么当我从 Windows 运行我的 Perl 脚本时找不到该文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3819069/