syntax error at ./friends.cgi line 31, near "elsif"

  • Belinda
  • Born
  • Born
  • Belinda
  • Posts: 2

Post 3+ Months Ago

Hi all;
I've been rying to find the error in my coding but I can't.

Can anyone see what i'm doing wrong
I would appricate it.Here is my coding
These are the error messages that I'm receiving

syntax error at ./friends.cgi line 31, near "elsif"
syntax error at ./friends.cgi line 57, near "}"
***********
Code: [ Select ]
#!/usr/bin/perl -w

use CGI qw/:standard/;

#print all headers
print header();
print start_html ( "Who is your friend?" );
#Display database content in case of starighforward GET
#if (request_method() eq "GET")
{
    open FRIENDS ,"friends.dat";
    print "<h3>These are my friends:</h3>\n";
    print"<table> \n " ;
    while ( <FRIENDS> )
    {
    chomp ;
    @fields = split / \ | / ;
    
    #Each record will be a self-contained form used for updating records
    print qq~\t<tr>\n~;
    print qq~\t\t<td><form method="POST" action="/cgi-bin/firends.cgi"> \n~ ;
    print qq~\t\t\t$fields[1] - $fields[2]\n~ ;    
    print qq~\t\t<td><input type="submit" value="Change" name="submit">~;
    print qq~<input type="hidden" name="key" value="$fields[0]">\n~;
    print qq~</form> \n~;
}
print "</table> \n";
}

#This request is an update is in progress
elsif (request_method() eq "POST")
{
    if (param('submit') eq "Change" )
    {
        open FRIENDS,"friends.dat";
        
        print "<h3>Changing record </h3>";
        print qq~<form method="POST" action="/cgi-bin/friends.cgi"> \ n~;
        while (<FRIENDS>)
        {
            @fields = split / \ | / ;
        
            #This tries to locate the desired record
            if ($fields[0] eq param('key'))
            {
            #print the chosen record in editable text boxes
            #everything except the product ID number is editable
            print qq~Name:<input type="text" name="name" value=$fields[1]" ><br>\n~;
            print qq~Phone: <input type="text" name="phone" value=$fields[2]" ><br>\n~;    
            print qq~<input type="hidden" name="key" value="$fields[0]" ><br>\n~;
            print qq~<input type="submit" name="submit" value="Save" > \n~;
            
            last;
            }
        }
        print "</form> \n" ;
    }
    else
    {
        #Open the database for exclusive read and write access
        open FRIENDS , " + <friends.dat" ;
        flock (FRIENDS,2);

        #Create a temporary file for updates and lock it
        open TEMP,"temp.dat";
        Flock (TEMP,2);

        while (<FRIENDS>)
        {
            @fields = split / \ | / ;
            
            #Create a temproray image of the databse with the updated record
            if ($fields[0] eq param('key'))
            {
                #Replace the edited record with the updated content
                print TEMP param( 'key' ) .  " | ". param( 'name' ) . " | " . param (
'phone' ) . "\n";
            }
            else
            {
                #All "other" records are processed unchanged
                print TEMP $_;
            }
        }
        #Overwrite the previous database with the updated version
        unlink "friends.dat";
        rename "temp.dat", "friends.dat";

        print"<h3>information saved!</h3>";

        close TEMP;
    }
}

#finish-up and cleanup
print end_html( );
close FRIENDS;
  1. #!/usr/bin/perl -w
  2. use CGI qw/:standard/;
  3. #print all headers
  4. print header();
  5. print start_html ( "Who is your friend?" );
  6. #Display database content in case of starighforward GET
  7. #if (request_method() eq "GET")
  8. {
  9.     open FRIENDS ,"friends.dat";
  10.     print "<h3>These are my friends:</h3>\n";
  11.     print"<table> \n " ;
  12.     while ( <FRIENDS> )
  13.     {
  14.     chomp ;
  15.     @fields = split / \ | / ;
  16.     
  17.     #Each record will be a self-contained form used for updating records
  18.     print qq~\t<tr>\n~;
  19.     print qq~\t\t<td><form method="POST" action="/cgi-bin/firends.cgi"> \n~ ;
  20.     print qq~\t\t\t$fields[1] - $fields[2]\n~ ;    
  21.     print qq~\t\t<td><input type="submit" value="Change" name="submit">~;
  22.     print qq~<input type="hidden" name="key" value="$fields[0]">\n~;
  23.     print qq~</form> \n~;
  24. }
  25. print "</table> \n";
  26. }
  27. #This request is an update is in progress
  28. elsif (request_method() eq "POST")
  29. {
  30.     if (param('submit') eq "Change" )
  31.     {
  32.         open FRIENDS,"friends.dat";
  33.         
  34.         print "<h3>Changing record </h3>";
  35.         print qq~<form method="POST" action="/cgi-bin/friends.cgi"> \ n~;
  36.         while (<FRIENDS>)
  37.         {
  38.             @fields = split / \ | / ;
  39.         
  40.             #This tries to locate the desired record
  41.             if ($fields[0] eq param('key'))
  42.             {
  43.             #print the chosen record in editable text boxes
  44.             #everything except the product ID number is editable
  45.             print qq~Name:<input type="text" name="name" value=$fields[1]" ><br>\n~;
  46.             print qq~Phone: <input type="text" name="phone" value=$fields[2]" ><br>\n~;    
  47.             print qq~<input type="hidden" name="key" value="$fields[0]" ><br>\n~;
  48.             print qq~<input type="submit" name="submit" value="Save" > \n~;
  49.             
  50.             last;
  51.             }
  52.         }
  53.         print "</form> \n" ;
  54.     }
  55.     else
  56.     {
  57.         #Open the database for exclusive read and write access
  58.         open FRIENDS , " + <friends.dat" ;
  59.         flock (FRIENDS,2);
  60.         #Create a temporary file for updates and lock it
  61.         open TEMP,"temp.dat";
  62.         Flock (TEMP,2);
  63.         while (<FRIENDS>)
  64.         {
  65.             @fields = split / \ | / ;
  66.             
  67.             #Create a temproray image of the databse with the updated record
  68.             if ($fields[0] eq param('key'))
  69.             {
  70.                 #Replace the edited record with the updated content
  71.                 print TEMP param( 'key' ) .  " | ". param( 'name' ) . " | " . param (
  72. 'phone' ) . "\n";
  73.             }
  74.             else
  75.             {
  76.                 #All "other" records are processed unchanged
  77.                 print TEMP $_;
  78.             }
  79.         }
  80.         #Overwrite the previous database with the updated version
  81.         unlink "friends.dat";
  82.         rename "temp.dat", "friends.dat";
  83.         print"<h3>information saved!</h3>";
  84.         close TEMP;
  85.     }
  86. }
  87. #finish-up and cleanup
  88. print end_html( );
  89. close FRIENDS;
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I don't really know perl, so I didn't look to closely at the code, but it looks like you have a comment symbol (#) before the if on line 10.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

I'm thinking that the first line in reference is the main culprit:

Code: [ Select ]
elsif (request_method() eq "POST")


shouldn't that be:
Code: [ Select ]
else if (request_method() eq "POST")


I'm not a wiz at Perl either, but that just didn't look right.


As a sidenote Belinda -- please use the bbCode Code brackets when posting code. 9 times out of ten it will screw up your post if you don't.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm pretty sure elsif is valid in perl, but if the first if is hidden by a comment then the error might be reported when the elsif was seen since it would have no preceding if.
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Well, you have the line "#if (request_method() eq "GET") " commented out, then the 'elsif' condition makes no sense. You must change it to 'if' or you have to uncomment the starting 'if' condition.

You must try to use accurate indentation in the text for finding errores easier.
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Code: [ Select ]
#!/usr/bin/perl -w

use CGI qw/:standard/;

#print all headers
print header();
print start_html ( "Who is your friend?" );
#Display database content in case of starighforward GET
if (request_method() eq "GET") {
  open FRIENDS ,"friends.dat";
  print "<h3>These are my friends:</h3>\n";
  print"<table> \n " ;
  while ( <FRIENDS> ) {
      chomp ;
      @fields = split / \ | / ;
     
      #Each record will be a self-contained form used for updating records
      print qq~\t<tr>\n~;
      print qq~\t\t<td><form method="POST" action="/cgi-bin/firends.cgi"> \n~ ;
      print qq~\t\t\t$fields[1] - $fields[2]\n~ ; 
      print qq~\t\t<td><input type="submit" value="Change" name="submit">~;
      print qq~<input type="hidden" name="key" value="$fields[0]">\n~;
      print qq~</form> \n~;
    }
    print "</table> \n";
}
elsif (request_method() eq "POST") {
  if (param('submit') eq "Change" ) {
   open FRIENDS,"friends.dat";
   
   print "<h3>Changing record </h3>";
   print qq~<form method="POST" action="/cgi-bin/friends.cgi"> \ n~;
   while (<FRIENDS>) {
     @fields = split / \ | / ;
   
     #This tries to locate the desired record
     if ($fields[0] eq param('key')) {
         #print the chosen record in editable text boxes
         #everything except the product ID number is editable
         print qq~Name:<input type="text" name="name" value=$fields[1]" ><br>\n~;
         print qq~Phone: <input type="text" name="phone" value=$fields[2]" ><br>\n~; 
         print qq~<input type="hidden" name="key" value="$fields[0]" ><br>\n~;
         print qq~<input type="submit" name="submit" value="Save" > \n~;
        
         last;
     }
   }
   print "</form> \n" ;
  }
  else {
   #Open the database for exclusive read and write access
   open FRIENDS , " + <friends.dat" ;
   flock (FRIENDS,2);

   #Create a temporary file for updates and lock it
   open TEMP,"temp.dat";
   Flock (TEMP,2);

   while (<FRIENDS>) {
     @fields = split / \ | / ;
    
     #Create a temproray image of the databse with the updated record
     if ($fields[0] eq param('key')) {
      #Replace the edited record with the updated content
      print TEMP param( 'key' ) .  " | ". param( 'name' ) . " | " . param ( 'phone' ) . "\n";
     }
     else {
      #All "other" records are processed unchanged
      print TEMP $_;
     }
   }
   #Overwrite the previous database with the updated version
   unlink "friends.dat";
   rename "temp.dat", "friends.dat";

   print"<h3>information saved!</h3>";

   close TEMP;
  }
}

#finish-up and cleanup
print end_html( );
close FRIENDS;
  1. #!/usr/bin/perl -w
  2. use CGI qw/:standard/;
  3. #print all headers
  4. print header();
  5. print start_html ( "Who is your friend?" );
  6. #Display database content in case of starighforward GET
  7. if (request_method() eq "GET") {
  8.   open FRIENDS ,"friends.dat";
  9.   print "<h3>These are my friends:</h3>\n";
  10.   print"<table> \n " ;
  11.   while ( <FRIENDS> ) {
  12.       chomp ;
  13.       @fields = split / \ | / ;
  14.      
  15.       #Each record will be a self-contained form used for updating records
  16.       print qq~\t<tr>\n~;
  17.       print qq~\t\t<td><form method="POST" action="/cgi-bin/firends.cgi"> \n~ ;
  18.       print qq~\t\t\t$fields[1] - $fields[2]\n~ ; 
  19.       print qq~\t\t<td><input type="submit" value="Change" name="submit">~;
  20.       print qq~<input type="hidden" name="key" value="$fields[0]">\n~;
  21.       print qq~</form> \n~;
  22.     }
  23.     print "</table> \n";
  24. }
  25. elsif (request_method() eq "POST") {
  26.   if (param('submit') eq "Change" ) {
  27.    open FRIENDS,"friends.dat";
  28.    
  29.    print "<h3>Changing record </h3>";
  30.    print qq~<form method="POST" action="/cgi-bin/friends.cgi"> \ n~;
  31.    while (<FRIENDS>) {
  32.      @fields = split / \ | / ;
  33.    
  34.      #This tries to locate the desired record
  35.      if ($fields[0] eq param('key')) {
  36.          #print the chosen record in editable text boxes
  37.          #everything except the product ID number is editable
  38.          print qq~Name:<input type="text" name="name" value=$fields[1]" ><br>\n~;
  39.          print qq~Phone: <input type="text" name="phone" value=$fields[2]" ><br>\n~; 
  40.          print qq~<input type="hidden" name="key" value="$fields[0]" ><br>\n~;
  41.          print qq~<input type="submit" name="submit" value="Save" > \n~;
  42.         
  43.          last;
  44.      }
  45.    }
  46.    print "</form> \n" ;
  47.   }
  48.   else {
  49.    #Open the database for exclusive read and write access
  50.    open FRIENDS , " + <friends.dat" ;
  51.    flock (FRIENDS,2);
  52.    #Create a temporary file for updates and lock it
  53.    open TEMP,"temp.dat";
  54.    Flock (TEMP,2);
  55.    while (<FRIENDS>) {
  56.      @fields = split / \ | / ;
  57.     
  58.      #Create a temproray image of the databse with the updated record
  59.      if ($fields[0] eq param('key')) {
  60.       #Replace the edited record with the updated content
  61.       print TEMP param( 'key' ) .  " | ". param( 'name' ) . " | " . param ( 'phone' ) . "\n";
  62.      }
  63.      else {
  64.       #All "other" records are processed unchanged
  65.       print TEMP $_;
  66.      }
  67.    }
  68.    #Overwrite the previous database with the updated version
  69.    unlink "friends.dat";
  70.    rename "temp.dat", "friends.dat";
  71.    print"<h3>information saved!</h3>";
  72.    close TEMP;
  73.   }
  74. }
  75. #finish-up and cleanup
  76. print end_html( );
  77. close FRIENDS;
  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

perl's lack of a switch function leads to unreadable code again, lol.


:lol:
  • nuclei
  • Graduate
  • Graduate
  • User avatar
  • Posts: 147
  • Loc: On a mountain

Post 3+ Months Ago

it is perfectly readable. his error is this line

#if (request_method() eq "GET")


he cant call an elsif if he does use the starting if

pretty simple for anyone who knows more than just php :P
  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

nuclei wrote:
it is perfectly readable. his error is this line

#if (request_method() eq "GET")


he cant call an elsif if he does use the starting if

pretty simple for anyone who knows more than just php :P


LOL !


I didn't mean to say it was completly illegible, it could just look a lot better using 'switch', more linear.

google wrote:
perl is internet yiddish



In sh I can understand using if / else / elsif until eternity, but perl really made a mistake of excluding a switch construct from its stock install.
;)

I program perl fulltime, so you may note that some of my comments regarding it are kinda .... je ne sais quois 'smart assed' ;)
  • nuclei
  • Graduate
  • Graduate
  • User avatar
  • Posts: 147
  • Loc: On a mountain

Post 3+ Months Ago

phpSelectah wrote:
nuclei wrote:
it is perfectly readable. his error is this line

#if (request_method() eq "GET")


he cant call an elsif if he does use the starting if

pretty simple for anyone who knows more than just php :P


LOL !


I didn't mean to say it was completly illegible, it could just look a lot better using 'switch', more linear.

google wrote:
perl is internet yiddish



In sh I can understand using if / else / elsif until eternity, but perl really made a mistake of excluding a switch construct from its stock install.
;)

I program perl fulltime, so you may note that some of my comments regarding it are kinda .... je ne sais quois 'smart assed' ;)


unfortunately, so do I. I am trying to get away from coding tho :P

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 73 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.