-2

I can't seem to figure out why I'm getting this error when trying to run my .pl script.

Here is the script:

# 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");

Test:

diff.pl test.patch -s

Output:

Scalar found where operator expected at C:\utils\diff.pl line 18, near

")

$stagedArg"

(Missing operator before $stagedArg?) syntax error at C:\utils\diff.pl line 18, near ")

$stagedArg " syntax error at C:\utils\diff.pl line 21, near ")

if" Execution of C:\utils\diff.pl aborted due to compilation errors.

Execution of C:\utils\diff.pl aborted due to compilation errors.

Can someone shed some light?

4
  • 11
    Don't write Perl scripts without use strict; and use warnings;.
    – Quentin
    Feb 11, 2013 at 16:45
  • 2
    String comparison id done with eq not with ==.
    – dgw
    Feb 11, 2013 at 16:49
  • 1
    @Quentin, he would get the same, newbie-cryptic, error message using strict and warnings. Is Perl really looking for "an operator" there? Or is it looking for a brace (or block)?
    – Axeman
    Feb 11, 2013 at 17:07
  • 2
    The perl compiler doesn't handle C code very well
    – Borodin
    Feb 11, 2013 at 19:04

1 Answer 1

9

Perl if syntax is:

if (condition) {
    statements;
}

You can't omit the curly braces.


You might find use diagnostics; useful. Given a simple test script:

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

We get:

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

(F) Probably means you had a syntax error. Common reasons include:

  • A keyword is misspelled.
  • A semicolon is missing.
  • A comma is missing.
  • An opening or closing parenthesis is missing.
  • An opening or closing brace is missing.
  • A closing quote is missing.

Often there will be another error message associated with the syntax error giving more information. (Sometimes it helps to turn on -w.) The error message itself often tells you where it was in the line when it decided to give up. Sometimes the actual error is several tokens before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to see if the error went away. Sort of the cybernetic version of 20 questions.

Uncaught exception from user code:
    syntax error at - line 5, near ") 
        print"
    Execution of - aborted due to compilation errors.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.