djaesone has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to run a script that I got from a book. I typed it myself from the book, using Notepad, and then save it as a .pl file.

When I tried to execute it, I got the following error:

syntax error at file.pl line 26, near "{ else" syntax error at file.pl line 30, near "}" Execution of file.pl aborted due to compliation errors.

Lines 18-34 are:

if ($res->is_success) {</br> my $fh = new FileHandle ">$filename"; if (defined $fh) { print $fh $res->content; $fh->close; } else { print STDERR "could not open file $filename: $!\n"; } { else } print STDERR $res->status_line, "\n"; } } } $station = 'mn'; for ($year = 00; $year <= 04; $year++) { my $filename = '200' . $year . $station . 'zip';
I will say that I am new to Perl, but having gotten this directly from the book, I wasn't thinking I needed to be a master. After some initial research, I inserted  use Diagnostics; at the top, but I got the same error.

Any idea what this means? In the perldiag documentation, it just says that this message means the execution ended, but I kinda figured that much. If there's somewhere else I can go to do my own research, just point me in that direction, and I'll take it from there.

Thanks!

Replies are listed 'Best First'.
Re: Syntax Error
by idsfa (Vicar) on Mar 19, 2006 at 06:08 UTC

    Those lines (26 & 27) should read:

    } else {

    Note the braces being flipped from the way you have given. If the book really has them that way, the book is wrong.

    You can see this for yourself by realizing that for every opening brace, bracket or parenthesis, there must be a matching closed one. If your editor is too simple to help you keep them balanced, you should count them yourself (and get a better editor ;-).

    The general syntax for an if loop in this case looks like:

    if ( CONDITION ) { BLOCK OF COMMANDS } else { BLOCK OF COMMANDS }

    Your example code has ifs within ifs, but still follows this pattern.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      AH! At my early stage in Perl understanding, I'm not sure even what the error message means, much less where to find the error in the script. But your response made total sense, and I appreciate your assistance with this.

        /boggle

        You'd find it on line 26, near the text "{ else".

        Doesn't get much simpler than that . . .

Re: Syntax Error
by jonadab (Parson) on Mar 19, 2006 at 10:45 UTC
    I just want to second the advice to get a text editor that has features like grouping-symbol matching and syntax highlighting. Notepad will get you by in a pinch if all you need to do is add one line to AUTOEXEC.BAT or CONFIG.SYS or something along those lines, but for extensive use like, writing source code, it's going to cause you a lot of pain in the long run.
Re: Syntax Error
by Viko (Acolyte) on Mar 19, 2006 at 15:34 UTC
    Using Notepad is (IMO) truly one of the biggest mistakes in programming - that applies to any language. I´ve used many editors over the years, my favorite being Programmer´s Notepad 2. It has many features that make a programmer´s life easier. Look it up in Google, I´m sure you´ll enjoy it.