Solved

Internal Server Error - stuck on syntax error

Posted on 2007-11-26
13
194 Views
Last Modified: 2010-04-21
I have a script to modify the date and I am stuck on a simple syntax error on my page..Below is my code, any Putty said it was:

syntax error at date.cgi line 38, near "){"
Execution of date.cgi aborted due to compilation errors.

Any suggestions?

Thanks,
Ryan

#!/usr/bin/perl

# date.cgi - converts a numeric date to a string

use CGI qw(:standard);

print "Content-type: text/html\n\n";
 

# declare variables for date

my (@months);

my ($tmon);

my ($i);

my ($a);

my ($date);

my ($m); # month

my ($d); # day

my ($y); # year
 
 

# assign input item to variable

# initiate all variables either start at 0 or use param()

$date = param('date');

$m    = 0;

$d    = 0;

$y    = 0;
 

# array of months

@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");

# tmon is 12

$tmon = @months;

$i    = 0;

$a    = 0;
 

#split date apart using split() on "/"

my @date = split("/", $date);

$m       = $date[0]; # array pos 0

$d       = $date[1]; # array pos 1

$y       = $date[2]  # array pos 2
 

# if for year add on "20 then XX"

if (length($y) == 2){

    $y= "20".$y;

}

# if for date

if (length($d) == 2){

    my @d = split("", $d);

    if ($d[0] == 0){

        $d = $d[1]

        }else{

        $d = join("", @d);

    }

}

#if for month - bring month array

if (length($m) == 2){

    my @m = split("", $m);

    if ($m[0] == 0){$m = $m[1]

        }else{

        $m = join("", @m);

    }

}
 

for ($i,$i<=$tmon,$i++)

{

    $a=$i-1;

    if ($m = $i){$m = $ms[$a];}

}

;

# print out final date

print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
Comment
  • 7
  • 4
  • 2
13 Comments
 
LVL 3

Expert Comment

by:BioI
ID: 20349717
you have to add a semicolon at the end of line 35
change:
$y       = $date[2]  # array pos 2
to
$y       = $date[2];  # array pos 2


0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20349727
Thank you after that I get - http://elan.champlain.edu/~rcoughlin32001/cgi-bin/date.cgi

Any suggestions how to get the output?

Thanks!

#!/usr/bin/perl

# date.cgi - converts a numeric date to a string

use CGI qw(:standard);

print "Content-type: text/html\n\n";
 

# declare variables for date

my (@months);

my ($tmon);

my ($i);

my ($a);

my ($date);

my ($m); # month

my ($d); # day

my ($y); # year
 
 

# assign input item to variable

# initiate all variables either start at 0 or use param()

$date = param('date');

$m    = 0;

$d    = 0;

$y    = 0;
 

# array of months

@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");

# tmon is 12

$tmon = @months;

$i    = 0;

$a    = 0;
 

#split date apart using split() on "/"

my @date = split("/", $date);

$m       = $date[0]; # array pos 0

$d       = $date[1]; # array pos 1

$y       = $date[2];  # array pos 2
 

# if for year add on "20 then XX"

if (length($y) == 2){

    $y= "20".$y;

}

# if for date

if (length($d) == 2){

    my @d = split("", $d);

    if ($d[0] == 0){

        $d = $d[1]

        }else{

        $d = join("", @d);

    }

}

#if for month - bring month array

if (length($m) == 2){

    my @m = split("", $m);

    if ($m[0] == 0){$m = $m[1]

        }else{

        $m = join("", @m);

    }

}
 

for ($i,$i<=$tmon,$i++)

{

    $a=$i-1;

    if ($m = $i){$m = $ms[$a];}

}

;

# print out final date

print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 3

Expert Comment

by:BioI
ID: 20349937
can you print out the $date-value, to get an idea how this looks like?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20349950
Still the same result, I dont get anything
# print out final date

print "Date: $date";

print $date;

print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 3

Expert Comment

by:BioI
ID: 20349969
even if you print it *immediately* after $date = param('date')?
because in that case, there is something wrong with the input form that comes before this cgi-script I guess...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20350007
If you try - http://elan.champlain.edu/~rcoughlin32001/date.html - only outputs some of it.
# assign input item to variable

# initiate all variables either start at 0 or use param()

$date = param('date');
 

# debug

print $date;

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
BioI earned 500 total points
ID: 20350012
by the way, you don't have to do something like this:
if (length($d) == 2){
    my @d = split("", $d);
    if ($d[0] == 0){
        $d = $d[1]
        }else{
        $d = join("", @d);
    }
}

perl will automatically interpret $d as an integer when it is used as the index of an array
This should work: [in the end, you used an non-existing array @ms instead of @months...]
#!/usr/bin/perl

# date.cgi - converts a numeric date to a string

use CGI qw(:standard);

print "Content-type: text/html\n\n";

 

# declare variables for date

my (@months);

my ($tmon);

my ($i);

my ($a);

my ($date);

my ($m); # month

my ($d); # day

my ($y); # year

 

 

# assign input item to variable

# initiate all variables either start at 0 or use param()

$date = param('date');

$m    = 0;

$d    = 0;

$y    = 0;

 

# array of months

@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");

# tmon is 12

$tmon = @months;

$i    = 0;

$a    = 0;

 

#split date apart using split() on "/"

my @date = split("/", $date);

$m       = $date[0]; # array pos 0

$d       = $date[1]; # array pos 1

$y       = $date[2]  # array pos 2

 

# if for year add on "20 then XX"

if (length($y) == 2){

    $y= "20".$y;

}
 

$m = $months[$m];
 
 

# print out final date

print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20350049
Thank you! That worked! So I didnt know that about how it will interpret it as an int as default! Thanks for all the help! Clearing this up for me.

So getting rid of that fixed it? Trying to understand my mistakes.

If you get some time I have one more easy one, but I am stuck - http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_22982316.html

Appreciate it
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
ID: 31410966
Great help!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20354713
Note that the code you accepted has a bug when it prints out the month.  Remember Perl arrays start at element 0, not 1.

Here's the corrected and simplified code:

#!/usr/bin/perl
# date.cgi - converts a numeric date to a string
use CGI qw(:standard);
print header;

my $date = param('date');

my @months   = qw(January February March April May June July August September October November December);

my ($mon,$day,$year) = split '/', $date;
$year="20$year" if (length($year) == 2);
my $month = $months[$mon-1];

print "<b>The date is:</b> $month $day, $year\n";
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20355296
Wow...how does that work? That little bit of code compared to that other stuff?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20355330
I have one more quick question:

>>my $month = $months[$mon-1];


Does that line take the array and subract one place? Due to how the arrays begin at 0? Or am I wrong?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20355366
You are correct in your assumption about the month array.

My code looks alot shorter due to:

1.  No comments.
2.  It's best practise to declare variables when they are used, not at the top of the script.
3.  Removed the need for a whole lot of intermediate variables that weren't needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now