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

发布于 2024-09-25 05:01:46 字数 914 浏览 6 评论 0原文

我有一个 Perl 脚本,它是使用 Perl 5.8 在 Linux 平台上构建的。但是现在我尝试在 Windows 平台命令提示符下使用相同的 Perl 版本运行 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 通过 at rgex.pl 第 7 行。
  • => rgex.pl 第 8 行没有这样的文件或目录。

请就代码提供一些建议。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2024-10-02 05:01:46

Perl 字符串中用双引号括起来的 \ 标记转义序列的开始,例如 \n 表示换行符,\t 表示制表符。由于您希望按字面意思处理 \ ,因此需要像 \\ 一样转义 \

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

因为您没有在字符串中插入任何变量,所以最好使用单引号:(

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

在单引号内, \ 并不特殊,除非下一个字符是反斜杠或单引号。)

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.)

行雁书 2024-10-02 05:01:46

这些错误消息非常清楚。它们准确地告诉您问题出在哪一行(与某些错误消息不同,它们通过“嘿,等一下!”告诉您 Perl 首先在哪一行)。

当您遇到此类问题时,请将程序缩减为仅存在问题的行并开始处理它们。首先从第一个错误开始,因为它们通常会级联到您稍后看到的错误。

当您想检查获得的值时,打印它以确保它是您所认为的那样:

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

这会很快向您表明 $file 存在问题,一旦您知道问题出在哪里,你就可以最大程度地解决问题。

这只是基本的调试技术。

另外,您还缺少相当多的基础知识,因此阅读一个好的 Perl 教程将对您有很大帮助。 perlfaq2perlbook。您遇到的许多问题都是学习 Perl 的前几章中要解决的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文