windows - 为什么当我从 Windows 运行我的 Perl 脚本时找不到该文件?

标签 windows perl command-prompt

我有一个 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/

相关文章:

multithreading - Perl线程模型

windows - 如何在 Windows 命令提示符下每 5 分钟安排一个任务?

c# - 帮助 : Call C# winforms dll from VB6 project?

Java - 如何检测某些多显示器设置的垂直位移?

python - 为什么我在 Windows 上从 Pillow 得到 "Not Enough Image Data",而相同的代码在 Linux 上运行良好?

windows - cmd/powershell : minimize all windows on your desktop except for current command prompt (console) or except for some particular window

windows - 在嵌套批处理循环中转义括号?

windows - openerp gtk 客户端 6.1 - 表单选项卡标签

perl - 从 Perl 数据结构中提取信息

perl - 为什么添加或删除换行符会改变这个 perl for 循环函数的方式?