Solved

Internal Server Error - stuck on syntax error

Posted on 2007-11-26
13
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
can you print out the $date-value, to get an idea how this looks like?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Accepted Solution

by:
BioI earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Great help!
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
Comment Utility
Wow...how does that work? That little bit of code compared to that other stuff?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

12 Experts available now in Live!

Get 1:1 Help Now