Python Kid
Python Kid

Reputation: 415

Perl Syntax Error - "syntax error line 15 near "} else" "

I've got a really annoying problem in Perl. Here is my code:

if $password eq "a_secret";
{
    foreach $var (sort(keys(%ENV))) {
        $val = $ENV{$var};
        $val =~ s|\n|\\n|g;
        $val =~ s|"|\\"|g; 
        print '${var}=\"${val}\"\n'
    };
}
else
{
print "<html><head><title>Unauthorized</title></head><h1>Unauthorized</h1><body>You do not have permission to access \printenv\printenv.pl on this server.</body></html>";
}

When I execute this code I get the following error message:

syntax error line 10 near "else"

Any Ideas? Note that some of the code has been removed.

Upvotes: 3

Views: 11281

Answers (3)

Starfish
Starfish

Reputation: 1091

if ($password eq "a_secret")
{
    foreach $var (sort(keys(%ENV))) {
        $val = $ENV{$var};
        $val =~ s|\n|\\n|g;
        $val =~ s|"|\\"|g; 
        print '${var}=\"${val}\"\n';
    }
}
else
{
print "<html><head><title>Unauthorized</title></head><h1>Unauthorized</h1><body>You do not have permission to access \printenv\printenv.pl on this server.</body></html>";
}

Upvotes: 4

zkar
zkar

Reputation: 61

The first if test clause must be enclosed with ( and ). Also, you cannot have ; after the if-condition in line 1.

Upvotes: 0

user507077
user507077

Reputation:

So many things to comment on. The error is in the very first line: you're missing parenthesis around the condition, and that ; at the end is superfluous.

Next: Writing \n inside a single-quoted string '...' will not do what you want.

Then, not really wrong, but not exactly good style either: print "..." } ; should have the ; and the } reversed.

Upvotes: 7

Related Questions