Perl syntax errors on hash generating script - perl

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.

Related

How to force EXPR instead of GLOB dereference?

use strict;
use warnings;
sub XX { 30 };
my $rnd = 3;
my $z = -XX * $rnd;
Give error: Can't use string ("3") as a symbol ref while "strict refs" in use
This does not help:
my $z = -XX * ($rnd);
I get next error:
Scalar found where operator expected at game4.pl line 7, near "* ($rnd"
(Missing operator before $rnd?)
syntax error at game4.pl line 7, near "* ($rnd"
Execution of game4.pl aborted due to compilation errors.
How to force EXPR instead of GLOB dereference?
A few options.
Explicitly tell Perl that you're not passing parameters to XX.
my $z = -XX() * $rnd;
Use the old-style calling conventions for subroutines (I really don't recommend this one).
my $z = -&XX * $rnd;
Define the subroutine as not taking parameters.
sub XX() { 30 };
But the best solution is to use the built-in constant pragma.
use constant XX => 30;

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.

Deciphering this syntax error

I cant seem to understand the reason for these syntax errors. The following is part of my code. I have strict and warnings implemented.
my $res = $s->scrape(URI->new($urlToScrape));
#warn Dump $res;
print "Show :".$res->{showtitle}[0];
my #sct;
if ( defined {season}[0] ) {
print $res->{season}[0];
#sct=split(' ', $res->{season}[0]);
} else {
my #spaa=split( {showtitle}[0], {fulltitle}[0] );
print "Couldnt find season num at proper position\n";
print $spaa[0]."\n";
print $spaa[1]."\n";
exit;
}
The error I get is:
$ ./htmlscrape.pl
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 43.
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 44.
syntax error at ./htmlscrape.pl line 37, near "}["
syntax error at ./htmlscrape.pl line 40, near "}"
syntax error at ./htmlscrape.pl line 46, near "}"
Execution of ./htmlscrape.pl aborted due to compilation errors.
There's syntax error in your code. Change:
if ( defined {season}[0] )
to
if ( defined $res->{season}[0] )
and
my #spaa=split( {showtitle}[0], {fulltitle}[0] );
to
my #spaa=split( $res->{showtitle}[0], $res->{fulltitle}[0] );
Also you are getting the warning
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 43.
That means you have declared two arrays with the same name #spaa in same scope. You'll probably find Coping with Scoping by Dominus worth reading. Pay particular attention to the section called "Lexical Variables".

execution of perl script

I am trying to run a perl script 'googly.pl' from the command line, and it is giving some errors. Here is the script and the errors. I have re-checked the script, but I am still unable to successfully run the script.
#!C:\Strawberry\perl\bin\perl.exe
# googly.pl
# A typical Google Web API Perl script
# Usage: perl googly.pl <query>
# Your Google API developer's key
my $google_key='';
# Location of the GoogleSearch WSDL file
my $google_wdsl = "C:/vhosts/phpcs5/GoogleSearch.wsdl";
use strict;
# Use the SOAP::Lite Perl module
use SOAP::Lite;
# Take the query from the command-line
my $query = shift #ARGV or die "Usage: perl googly.pl
<query>\n";
# Create a new SOAP::Lite instance, feeding it
GoogleSearch.wsdl
my $google_search = SOAP::Lite->service("file:$google_wdsl");
# Query Google
my $results = $google_search ->
doGoogleSearch(
$google_key, $query, 0, 10, "false", "", "false",
"", "latin1", "latin1"
);
# No results?
#{$results->{resultElements}} or exit;
# Loop through the results
foreach my $result (#{$results->{resultElements}}) {
# Print out the main bits of each result
print
join "\n",
$result->{title} || "no title",
$result->{URL},
$result->{snippet} || 'no snippet',
"\n";
}
Errors
Semicolon seems to be missing at C:/vhosts/phpcs5/googly.pl line 19.
Syntax error at C:/vhosts/phpcs5/googly.pl line 17, near "wsdl my "
Global symbol "$google_search" requires explicit package name at C:/vhosts/phpcs5/googly.pl line 17
Global symbol "$google_search" requires explicit package name at C:/vhosts/phpcs5/googly.pl line 19
Syntax error at C:/vhosts/phpcs5/googly.pl line 20, near "doGoogleSearch"
Execution of C:/vhosts/phpcs5/googly.pl aborted due to compilation errors.
my $query = shift #ARGV or die "Usage: perl googly.pl
<query>\n";
# Create a new SOAP::Lite instance, feeding it
GoogleSearch.wsdl
my $google_search = SOAP::Lite->service("file:$google_wdsl");
should be
my $query = shift #ARGV or die "Usage: perl googly.pl <query>\n";
# Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl
my $google_search = SOAP::Lite->service("file:$google_wdsl");

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

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.