New to perl; getting some errors I'm not sure about - perl

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?

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.

Related

Perl compilation problem that Perl::Critic does not see

I have this if statement that does not pass compilation:
my $string_var;
if ( $string_var eq "string_value1" || $string_var eq "string_value2" ) &&
(defined $ENV{VAR_NAME}) {
die { "error_message" => "error_message" }
}
Neither I nor Perl::Critic see a problem. But Perl says:
syntax error at /opt/app_protect/bin/../lib/perl/F5/BdCfg/Bundle.pm line 178, near ") &&"
syntax error at /opt/app_protect/bin/../lib/perl/F5/BdCfg/Bundle.pm line 181, near "}"
Can anyone help?
It's CentOS 7.6.1810 with Perl v5.16.3
From perlsyn:
The following compound statements may be used to control flow: ...if (EXPR) BLOCK
What you provide here is not
if (EXPR) BLOCK
but instead
if (EXPR1) && (EXPR2) BLOCK
This needs to be enclosed in parentheses in order to be a valid syntax, i.e.
if ((EXPR1) && (EXPR2)) BLOCK

Warning Message using the -e operator as the condition of a WHILE loop

Using warnings and strict. ActivePerl (vendor restricted) version 5.8.0 Build 806
Getting this warning message: Use of uninitialized value in numeric ne (!=) at D:\NIGHTLY\SamJudNighlty_ABS.pl line 589 (code snippet at bottom).
Actually multiple lines receive this warning as this construct is used frequently in the code. Inherited this code and just trying to clean it up. Not sure if this is an issue, but wanted to follow up on it.
From what I have researched: "-e returns 1 for true and '' for false, or the undefined value if the file doesn't exist.
587 $finFlag = $NightlyFlagPath . "\\FIN\\DONE";
588
589 while((-e $finFlag) != 1)
590 {
591 sleep(120);
592 print(PROGRESSFILE "\nWaiting for the Fin Nightly to finish" . getPCTime5());
593 }
The whole point of "true/false" return of many operators in Perl, -e certainly included, is that it need not be tested any further. You just need
while (not -e $finFlag) { ... }
The != equality operator compares its arguments numerically thus the warning when it gets undef to work with (or '', or any string). Which is precisely what happens when the file does not exist since -e then indeed returns undef, like other filetests do.
-e returns a true value if the file exists, and undef if an error occurred (incl the file not existing).
You are using undef in a numerical comparison, thus the warning. Use boolean arithmetic instead.
Fixed:
while (!-e $finFlag) {
sleep(120);
print(PROGRESSFILE "Waiting for the Fin Nightly to finish " . getPCTime5() . "\n");
}
However, that will loop forever if the file exists, but something prevents you from getting information about it (such as a permission problem). The following solves that problem:
while (1) {
last if -e $finFlag;
die("Can't stat \"$finFlag\": $!\n") if !$!{ENOENT};
sleep(120);
print(PROGRESSFILE "Waiting for the Fin Nightly to finish " . getPCTime5() . "\n");
}
Slightly clearer:
while (1) {
last if stat($finFlag);
die("Can't stat \"$finFlag\": $!\n") if !$!{ENOENT};
sleep(120);
print(PROGRESSFILE "Waiting for the Fin Nightly to finish " . getPCTime5() . "\n");
}

PERL syntax error near "= ) "

I have found this example but I get this error:
syntax error at ./test.pl line 11, near "= ) " Execution of ./test.pl
aborted due to compilation errors.
Line 11: while($line = ) {
The author of the script didn't prepare the script to be included in an HTML. The actual code was
while ($line = <INFILE>) {
See readline for details.

Perl: Error printing after do-while loop

I am making a concatenating do-while loop wherein each of the entered strings will be added to the main string $food. Here is the code:
do {
print("\nEnter your order(-1 to end): ");
$order = <>;
chop($order);
if ($order != -1) {
$food .= " ".$order;
print($food);
}
} while ( $order != -1)
print ($food); #PROBLEM HERE!
The problem is that whenever I put print ($food) outside the loop, a syntax error for this line appears plus an Execution of file.pl aborted due to compilation errors message.
The code works once I put print ($food) inside the loop but I am curious to why this error is happening.
Your line before last print has syntax error - it does not end with ;.
Just add it and it should work.
A thing worth mentioning in addition to the syntax error: I see that you you start the string with a newline. While this is fine, it might be considered "better" to end strings with the newline before printing, as printing a newline would result in flushing the STDOUT buffer right away. In most cases, this is the behavior you want, as it prevents delayed output and text flickering (especially if printing a lot).

Perl syntax errors on hash generating script

I am getting the following errors when running the code:
syntax error line 5, near "use Digest::MD5
sub makeKey
"
syntax error at line 8, near "}"
syntax error at line 15, near ")
}"
Execution aborted due to compilation errors.
My script:
use lib '/home/me/Desktop/pm/MD5.pm';
use Digest::MD5
sub makeKey
{
my ($strPassword, $strRndk);
$strKey = uc(md5Hash($strPassword)) + $strRndk + "Y(02.>'H}t\":E1" + md5Hash($strKey);
return $strKey;
}
sub md5Hash
{
my ($strPassword);
$strMd5 = md5_hex($strPassword);
return substr($strMd5, 16, 16) + substr($strMd5, 0, 16);
}
makeKey('test', '1A2B3C');
Use Digest::MDd5 needs to end with a semicolon.
Concerning your third (and final?) problem:
"Undefined subroutine &main::md5_hex called on line 14"
Digest::MD5 doesn't export md5_hex (or anything else) by default, you have to explicitly tell it that it should export md5_hex:
use Digest::MD5 qw(md5_hex);
or use the full Digest::MD5::md5_hex name.