Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Syntax errors with {s

by Anonymous Monk
on Oct 10, 2004 at 17:42 UTC ( [id://398015]=perlquestion: print w/replies, xml ) Need Help??

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

I keep getting syntax errors with the code below. It's saying there are errors before the first ELSE and probably every else thereafter.

Can someone debug this for me?

if (param()) { my $name = param("name"); my $email = param("email"); my $phone = param("phone"); my $url = param("url"); my $requests = param("requests"); if ($name ne "") { if ($email ne "") { if ($requests ne "") { print "very good!"; } else { print "please leave a message"; } else { print "please enter your email"; } else { print "please enter your name"; } }

Replies are listed 'Best First'.
Re: Syntax errors with {s
by Limbic~Region (Chancellor) on Oct 10, 2004 at 17:46 UTC
    Anonymous Monk,
    You were missing a couple of close braces
    #!/usr/bin/perl use strict; use warnings; sub param { } if ( param() ) { my $name = param("name"); my $email = param("email"); my $phone = param("phone"); my $url = param("url"); my $requests = param("requests"); if ( $name ne "") { if ($email ne "") { if ($requests ne "") { print "very good!"; } else { print "please leave a message"; } } # You were missing me else { print "please enter your email"; } } # You were missing me else { print "please enter your name"; } }

    Cheers - L~R

Re: Syntax errors with {s
by sulfericacid (Deacon) on Oct 10, 2004 at 17:46 UTC
    I often have trouble with nested if/else statements as well and sometimes they can be a bugger to fix. But it looks like you're only missing two }'s.

    You're not closing your first and your second IF, that's where the problems occur. So all I did was added a closed bracket after the first and second ELSE. Please see the script below.

    if (param()) { my $name = param("name"); my $email = param("email"); my $phone = param("phone"); my $url = param("url"); my $requests = param("requests"); if ($name ne "") { if ($email ne "") { if ($requests ne "") { print "very good!"; } else { print "please leave a message"; } } else { print "please enter your email"; } } else { print "please enter your name"; } }


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
      sometimes they can be a bugger to fix.

      I can see such problems being hard to fix when (and only when) the brackets are not lined up like in the solution you gave...

        On guard!

        Each coder has their own way they prefer to see and write code. I much prefer doubling up the closures then break into a new line.

        Many might argue with this being harder to read, but I've been pretty constant with it since the very beginning.



        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid
Re: Syntax errors with {s
by davido (Cardinal) on Oct 11, 2004 at 02:12 UTC

    Others have mentioned that you're just missing a couple closing brackets. That's accurate. I just wanted to mention some tricks (good sound principles actually) that you can use to avoid this problem in the future.

    First (and this has already been mentioned but deserves more attention), develop a style where it's easy to spot mismatched brackets of all kinds. Your example code is getting there, but it let you down still. I kind of like cuddled brackets like this:

    if (...) { #stuff } else { #more stuff. }

    ...or...

    if ( .... ) { if ( ... ) { # stuff } else { # more stuff } }

    To me that keeps it pretty clear, but everyone has their own minor variations on such themes. Consistancy helps.

    Another thing to try is a syntax-highlighting editor. Well-done syntax highlighting will help you to see when brackets don't match. The editor I use actually draws a little dotted line between matching brackets whenever my cursor is resting on a bracket. If the line doesn't lead anywhere, I can quickly see that's where I've got a problem (I use SciTE, which I think you can Google for. Other good (and possibly better) editors will have other techniques that help. But a good editor really can be a step in the right direction. Once you become familiar with the editor, it's easy to look at a piece of code and see where the syntax highlighting looks messed up. ...and that's where you can focus your efforts.

    Another technique is to just test-compile the script. This is very helpful when you're working in a CGI environment where it may otherwise take several steps to get at the error messages. To test compile code, and see what errors the trial compilation may be generating, you can, at the command line, type: perl -c scriptname

    Doing that will get Perl to attempt to compile the script, and if there's a problem, you'll see the errors immediately, rather than having to sift through webserver error logs.

    Hope these tips help...


    Dave

Re: Syntax errors with {s
by tachyon (Chancellor) on Oct 10, 2004 at 22:46 UTC

    This looks like a CGI. The technique of building up an error string and reporting it if present may be of use:

    my $errstr = ''; for my $param( qw( name email phone url requests ) ) { $errstr .= "Please enter your $param<br>\n" unless defined param($ +param); } # if $errstr is true we have some errors, so tell user if ( $errstr ) { print "Content-Type: text/html\n\n"; print "<p>Please click back and correct these error(s):\n<p>" . $e +rrstr; exit 0; } # got data OK so continue processing

    cheers

    tachyon

Re: Syntax errors with {s
by TedPride (Priest) on Oct 10, 2004 at 22:45 UTC
    I don't like all that extra white space myself. Makes it hard to fit a good chunk of code into your window, and therefore much harder to track large numbers of nested statements. The above can be compressed into the following while not messing up readability:
    if (param()) { my $name = param("name"); my $email = param("email"); my $phone = param("phone"); my $url = param("url"); my $requests = param("requests"); if ($name ne "") { if ($email ne "") { if ($requests ne "") { print "very good!"; } else { print "please leave a message"; } } else { print "please enter your email"; } } else { print "please enter your name"; } }
    Or even the following, since you don't really need separate variables for each param value:
    if (param()) { if (param('name') ne "") { if (param('email') ne "") { if (param('requests') ne "") { print "very good!"; } else { print "please leave a message"; } } else { print "please enter your email"; } } else { print "please enter your name"; } }
Re: Syntax errors with {s
by nimdokk (Vicar) on Oct 11, 2004 at 10:01 UTC
    As people have already mentioned the problem with closing braces, I'll simply mention another technique I use to help prevent that problem. Whenever I open a brace (as with an if-statement), I close it right away (before inserting any code) and put a comment on the line with the closing brace - indicating which brace it is supposed to be close (helpful with nested if statements). For example:

    if ($var < 0) { <do something> if ($var2 == 1) { <do something else> }#close second if } elsif ($var == 0) { <do this other thing> } else { <do something completely different> }#close first if

    Its really a matter of preference, find something that works and stick with it. Keeping things indented and in line helps as well. Marking the closing braces can be used with for loops, code blocks or anything like that.

      That's precisely what I do except I extend that idea to include all paired characters.

      I very rarely get any syntax errors using this method.

      It is also independent of the programmer's choice of code layout.

      Regards,

      PN5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://398015]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-06-15 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.