Deciphering this syntax error - perl

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

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

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.

compilation error while defining a variable scope

Could someone kindly point me why this snippet is not compiled:
my $crond = "/etc/init.d/crond";
if( -e $crond ) {
my $d = "d";
}
my $crond = "/etc/init.d/cron$d";
Error:
"my" variable $crond masks earlier declaration in same scope at /home/andrew/sandbox/processes2cron.pl line 27.
Global symbol "$d" requires explicit package name at /home/andrew/sandbox/processes2cron.pl line 27.
I tried different variations with 'my' but still the scope is defined uncorrectly. Thanks.
You have already declared the variable $crond in the first line of your code. By re-declaring it on line 5, you will lose the previous value. In this case, removing the my on line 5 will stop the warning.
The variable $d is declared within the scope of the if block. This means that it is only accessible until the end of the if block. You then try to refer to it outside the if block, which causes the error. To fix this, declare $d before the if statement in the outer scope.
my $crond = "/etc/init.d/crond";
my $d;
if( -e $crond ) {
$d = "d";
}
$crond = "/etc/init.d/cron$d";
It's just as the error message says. You're redeclaring $cron within the same scope, and $d is only defined within the if block, so the compiler expects $d to be a global variable when you use it on the last line, and complains when it can't find it.

Perl syntax error: Bareword found where operator expected

As the title suggests how could I accomplish this?
I have been following a tutorial, but I get a syntax error:
Bareword found where operator expected at arrays_and_variables.pl line
26, near "$2names"
(Missing operator before names?) syntax error at
arrays_and_variables.pl line 26, near "$2names " Execution of
arrays_and_variables.pl aborted due to compilation errors.
The code I have so far is:
#names = ('james','dylan','max');
# join elements of array into a schalar variable.
$2names = join ('', #names);
print $s2names;
2names is an invalid variable name. Names can't start with a number—they have to begin with a letter or an underscore.

Perl syntax error, but I can't find it for the life of me

This error is angering me. I can't see anything near those lines with a parenthesis error or missing brackets. Someone give me a hand? This is my first post, forgive me if the formatting is off; I think I got it right.
EDIT: line 87, the ');' error, is this line: select(SEXTANT_DAEMON_LOG);
syntax error at -edited- line 87, near ");"
syntax error at -edited- line 92, near "if"
syntax error at -edited- line 99, near "if"
Unmatched right curly bracket at -edited- line 102, at end of line
syntax error at -edited- line 102, near "}"
syntax error at -edited- line 109, near "}"
syntax error at -edited- line 120, near ");"
BEGIN not safe after errors--compilation aborted at -edited- line 122.
This is the code near the error (full code here):
$MAIN_DBH = getConnection('Main');
$fs_logfile = getCSConfigValue($MAIN_DBH, 'Log', 'Sextant Update Daemon') or die "pid$$[" . localtime(time()) . "] Main dbh error: " . DBI::errstr;
open(SEXTANT_DAEMON_LOG, '>>', $fs_logfile) or die "pid$$[" . localtime(time()) . "] unable to open log file '$fs_logfile'\n";
$tmp = select(SEXTANT_DAEMON_LOG);
$| = 1;
select(SEXTANT_DAEMON_LOG);
Perl isn't giving a very good error message, but what it's actually complaining about is that "pid$$[" looks like an invalid attempt to access the array #$. Try replacing it with "pid$$\[".
The way I found that was by inserting __END__ near the reported location of the first error. I moved it up and down until I found the first line that caused an error, which was
$fs_logfile = getCSConfigValue($MAIN_DBH, 'Log', 'Sextant Update Daemon') or die "pid$$[" . localtime(time()) . "] Main dbh error: " . DBI::errstr;
Then I tried adding the backslash, and it fixed the error.
Note: perl -c is very useful in situations like this.