-2

我似乎无法弄清楚为什么在尝试运行我的 .pl 脚本时会出现此错误。

这是脚本:

# mini script for creating diff files in a single directory

# directory of patch files
$patchDir = "c:\oc\Patch Files";

if ($#ARGV != 1 && $#ARGV != 2)
{
    print "Usage: diff <file name> [-s]\n";
    print "Example:  \n";
    print "   |Diff relative to Index (staged)|  :  diff MOM00123456_1.patch \n";
    print "   |Diff Staged|                      :  diff MOM00123456_1.patch -s \n";
    exit;
}

$fileName = $ARGV[0];

if ($#ARGV == 2)
    $stagedArg = $ARGV[1];

if ($stagedArg)
    if ($stagedArg == "-s" || $stagedArg == "-S")
        system("git diff --staged --full-index > $fileName $patchDir");
    else
    {
        print "Unknown argument:  $stagedArg\n";
        exit;
    }
else
    system("git diff --full-index > $fileName $patchDir");

测试:

diff.pl test.patch -s

输出:

标量在 C:\utils\diff.pl 第 18 行附近的运算符预期位置找到

")

$stagedArg"

(在 $stagedArg 之前缺少运算符?) C:\utils\diff.pl 第 18 行,“附近”的语法错误)

$stagedArg " C:\utils\diff.pl 第 21 行的语法错误,靠近 ")

如果” C:\utils\diff.pl 的执行由于编译错误而中止。

由于编译错误,C:\utils\diff.pl 的执行中止。

有人可以阐明一下吗?

4

1 回答 1

9

Perlif语法是:

if (condition) {
    statements;
}

你不能省略花括号。


你可能会觉得use diagnostics;有用。给定一个简单的测试脚本:

use strict;
use warnings;
use diagnostics;
if (1) 
    print 1;

我们得到:

syntax error at - line 5, near ") 
    print"
Execution of - aborted due to compilation errors (#1)

(F) 可能意味着您有语法错误。常见原因包括:

  • 关键字拼写错误。
  • 缺少一个分号。
  • 缺少一个逗号。
  • 缺少左括号或右括号。
  • 缺少左大括号或右大括号。
  • 缺少结束引号。

通常会有另一条与语法错误相关的错误消息,提供更多信息。(有时打开 -w 会有所帮助。)错误消息本身通常会告诉您当它决定放弃时它在哪里。有时实际错误是在此之前的几个标记,因为 Perl 擅长理解随机输入。有时,行号可能会产生误导,而一旦在一个蓝月亮上,找出触发错误的唯一方法就是反复调用 perl -c ,每次都砍掉一半的程序,看看错误是否消失了。20 个问题的控制论版本。

Uncaught exception from user code:
    syntax error at - line 5, near ") 
        print"
    Execution of - aborted due to compilation errors.
于 2013-02-11T16:46:07.017 回答