Gossamer Forum
Home : General : Perl Programming :

help with a script

Quote Reply
help with a script
Created a script some time ago. Had almost forgotten all about it when I
found it on my computer, hidden in the deepest caves of Windows 98.
Well, I had given up on the script and now I dont know why. It is a link game
archive. I can search after some links and info I got in a file and now I'm
having problems with the "post new" script.
Its quite long so take your time and help me find out what is wrong. I'm no
expert and terrible at finding errors in script. Is there another smart, fast en
secure way to find errors inside scripts?
Please let me know.

The script(add.cgi):

#!/usr/bin/perl

#
#
# Title: Games Pages
#
# (C) Copyright 2002-2004
# All rights reserved.


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

if ({'QUERY_STRING'} eq "write") {
&parseform;
✓
&write;
}
else {

print <<FORMfields;
<html><head><title>Add Games</title></head>
<body>
<h2 align="Center">Add Games</h2>

Wanna add a cool game to our archive? No problem!<br>
Just use the form belove. Fill in the required information(the ones with a
<font color="light blue">*</font> next to it)<br>
and press the Send button! Easy!<p>
<form action="add.cgi?write" method="Post">

<STYLE TYPE="text/css">
TD {
font-size:12
}

</STYLE>
<TABLE BORDER="0" CELLSPACING="0">
<TR>
<TD>The games name:<BR><INPUT TYPE="text" NAME="name">&nbsp;<FONT
COLOR="lightblue">*</FONT></TD>
<TD>The games link:<BR><INPUT TYPE="text" VALUE="http://"
NAME="link">&nbsp;<FONT COLOR="lightblue">*</FONT></TD>
</TR>
<TR>
<TD>Picture address:<BR><INPUT TYPE="text"
VALUE="http://www.myhost.com/gamepicture.jpg" NAME="picture"></TD>
<TD>Your rating of the game<BR>(from 1 to 10 where 10 is
best):<BR><INPUT TYPE="text" NAME="rating">&nbsp;<FONT
COLOR="lightblue">*</FONT></TD>
</TR>
<TR>
<TD>Clicks to start with? (between 1-50)<BR><INPUT TYPE="text"
NAME="clicks"></TD>
<TD>In what category is the game?<BR><SELECT SIZE="1" NAME="cat">
<OPTION>Sport
<OPTION>Action
<OPTION>Adventure
<OPTION>Skiing
<OPTION>Strategy
<OPTION>Car game
<OPTION>Other
</SELECT>&nbsp;<FONT COLOR="lightblue">*</FONT></TD>
</TR>
<TR>
<TD VALIGN="middle">Description (Max 150 letters):<BR><TEXTAREA
WRAP="physical" ROWS="4" COLS="20" NAME="description"></TEXTAREA>&nbsp;<FONT
COLOR="lightblue">*</FONT></TD>
</TR>
<TR>
<TD><INPUT TYPE="submit" VALUE="Send">&nbsp;<INPUT TYPE="reset"
VALUE="Erase"></TD>
</TABLE>

</form><p>




FORMfields

} # stop else


# the subroutines..

# parse form
sub parseform {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split (/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\n/ /g;
$value =~ s/\r//g;
$value =~ s/\cM//g;
$FORM{$name} = $value;
}

# the check sub

sub check {
$picture = $FORM{'picture'};
$rating = $FORM{'rating'};
$click = $FORM{'clicks'};
$description = $FORM{'description'};
$lenghtofdesc = length ($description);

# If no title, boooom, error!
if ($FORM{'name'} eq "") {
error("Please write in a name for the game. You must fill in all the fields
marked with a <font color=lightblue>*</font>\n");
}
# Checking if a url has been posted.
elsif ($FORM{'link'} eq "" or $FORM{'link'} eq "http://") {
error("It seems to be a problem with the link to the game's webpage. You
must fill in all the required fields. \n");
}

# Test if the visitor wants to use a picture or not.
if ($FORM{'picture'} eq "http://www.myhost.com/gamepicture.jpg" or $FORM
{'picture'} eq "") {
$picturelink = "noimage.jpg";
}
# Check if the visitor has posted a jpg or a gif image. If not, error.
elsif ($picture !~ /\.jpg$/i or $picture !~ /\.gif$/i) {
error("If you want to use a picture it must be a .jpg or .gif picture. We
don't support .bmp or other types.\n");
}
# Let's find out what the rating is set to and if it has crossed the borders.
if ($FORM{'rating'} eq "") {
error("Please set a rating to the game. A number between 1 and 10 will do
fine. 10 is best, 1 is worst.\n");
}
elsif ($rating !~ /\d/) && ($rating < 1) {
error("You have to write a number between 1 and 10. A game can't be better,
or worse than that!");
}

# Lets take the clicks field:
if ($FORM{'clicks'} eq "") {
$clicks = 1;
}
elsif ($click < 1 or $click > 50) {
error("You'll have to fill in a number between 1 and 50. If you leave the
field blank, the click will be set to 1.\n");

}

# And the last: the description field.

if ($FORM{'description'} eq "") {
error("You must fill in a description! And don't let it be longer than 150
letters.\n");
}
elsif ($description !~ /\w/) {
error("You are using a sign that you ain't allowed to use. Go back and just
use letters and numbers.\n");
}
elsif ($lenghtofdesc > 150) {
error("To long! The description is over 150 characters. Please go back and
try again!\n");
}


} # the sub's finish

####################################

# the write sub
sub write {
# First open the file to get the next number.
$data_file = "data.db";
open (DATA, "$data_file") || error("Could not open $data_file: $!");
@raw_data = <DATA>;
close(DATA);

$lastgame = pop(@raw_data);
chop($lastgame);
($stocknum1,$name1,$link1,$picture1,$description1,$rating1,$clicks1,$cat1)
=split(/\|/, $lastgame);
$newstockmun = $stocknum1 + 1;


open(DATA, ">>data.db") or error("Could not open data.db for writing.
Reason: $!\n"); # open or error.
flock(DATA,2); # lock the file
seek(DATA,0,2); # set the pointer to the end
print DATA "$newstocknum|$FORM{'name'}|$FORM{'link'}|$FORM{'picture'}|$FORM
{'description'}|";
print DATA "$FORM{'rating'}|$FORM{'clicks'}|$FORM{'cat'}";
}

sub error {
my($msg) = @_;
print "<h2>Error</h2>\n":
print $msg;
exit;
}

It has been uploaded in ASCII and chmod 755.

Still cant get it right. It is a software error that comes up when using the
codes above:

Can't continue after import errors at [my path is here but I do not want to
show it] line 2 BEGIN failed--compilation aborted at [my path is here but I do
not want to show it] line 2.

Last edited by:

Andy: Mar 29, 2011, 2:42 AM
Quote Reply
Re: [perlman] help with a script In reply to
aint it nobody who can help me?
Please! I know the script is long, but please answer!
I wanna get this script right!
Quote Reply
Re: [perlman] help with a script In reply to
You've waited a couple of hours for a reply and it is a Sunday too. Be patient.

You have a syntax on the second line to begin with.

Last edited by:

Paul: Feb 16, 2003, 11:35 AM
Quote Reply
Re: [Paul] help with a script In reply to
Sorry.

But anyway:
I didn't posted the right version. The one I wrote about had a
Code:
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
at the top.


Execution of /home/p/pt/pt-home/public_html/mypage/games_page/addgame.cgi aborted due to compilation errors.

This comes up now. Changed the title of the file yeah. If you noticed. Sending the new script as a attachment. So the post wont get the long. Take a look at it if you want.
Thanks.

Last edited by:

brewt: May 12, 2011, 12:17 PM
Quote Reply
Re: [perlman] help with a script In reply to
So you didn't see the list of errors, like;

Quote:
syntax error at test.cgi line 130, near ") &&"
syntax error at test.cgi line 191, near ""<h2>Error</h2>\n":"
Missing right curly or square bracket at test.cgi line 196, at end of line
Execution of test.cgi aborted due to compilation errors.


First error I see, from the above, is;

Code:
elsif ($rating !~ /\d/) && ($rating < 1) {

You have it seperated for some reason. Try;

Code:
elsif ($rating !~ /\d/ && $rating < 1) {


Also, on line 190, why do you have;

Code:
my($msg) = @_;

? You need;

Code:
my $msg = $_;

All the way through your script, you also use stuff like;

Code:
if ($FORM{'description'} eq "") {

You could get around this with something like;

Code:
if (!$FORM{'description'}) {


Another error;

Code:
print "<h2>Error</h2>\n":

Notice the :, instead of the ; Wink


I don't want to really go on....but here is another thing you could get rid of, to clean up your code;

Code:
# parse form
sub parseform {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split (/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\n/ /g;
$value =~ s/\r//g;
$value =~ s/\cM//g;
$FORM{$name} = $value;
}

I used to use code like that, but since found CGI.pm. You could cut all that code down, and make your script faster, by using something like this at the top of your script;

Code:
use CGI;
$IN = new CGI;

# call variables like this
my $var = $IN->param('param_name');

# to print a content header, you could also use;
print $IN->header();

Anyway...I'm gonna stop here, and let you do some of the debugging yourself Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
Quote:
You need;


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------

my $msg = $_;

Are you sure ;)
Quote Reply
Re: [perlman] help with a script In reply to
The bug I referred to on line two is still there:

Code:
if ({'QUERY_STRING'} eq "write") {

You'll probably want $ENV to preceed {'QUERY_STRING'} Wink
Quote Reply
Re: [Paul] help with a script In reply to
In Reply To:
You need;


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------

my $msg = $_;

Are you sure ;)

Whooops....maybe not. Its;

Code:
my $msg = shift;

My boo boo Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] help with a script In reply to
In Reply To:
The bug I referred to on line two is still there:

Code:
if ({'QUERY_STRING'} eq "write") {

You'll probably want $ENV to preceed {'QUERY_STRING'} Wink

Moving over to CGI.pm is still a hell of a lot easier IMO. I still remember when it was you trying to persuade me over to CGI.pm, Paul Laugh

BTW Perlman: please don't PM me in an attempt to get attention. Flattering doesn't work either Wink If I find time, I will answer as many of the posts as I can. Programming/installing is my job, so obviously paid work comes first.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
The elsif doesn't make sense either:

Code:
elsif ($rating !~ /\d/ && $rating < 1) {

That's saying to run that block of code if the rating does not include digits and is less than one. If a rating is not a digit then it can't be less than one and if a rating is a digit then it won't match.

Last edited by:

Paul: Feb 17, 2003, 7:42 AM
Quote Reply
Re: [Paul] help with a script In reply to
Mmmm....now I see why he gave up on it. Personally, I would scrap the script, and start from scratch. You have probrably picked up a few better habits now...and you can take on some of mine/Pauls suggestions.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
Wow!
Was hoping for one simple reply and when I look at my post it said 10 replies!
Im the flattered one! Cool
I have this habit of doing alot of mistakes in my scripts. Don't actually know why? Crazy
I will make the corrections you said and about that
Code:
my($msg) = @_;
.
Thats what I have learned from the beggining! CGI Programming 101 by Jacqueline D. Hamilton! Great book! That code is used throughout all of the book.
Sure its wrong?
And about CGI.pm. Have considered to use it.
I'm not sure if I want to use it yet. Want to learn how to make a real script without modules first then maybe start using them afterwards. I might make one myself for personal use? hmmm....good thought perlman!
Thanks Andy and Paul!
And sorry for the rude way that I contacted you through a message system! silly me! Tongue
Quote Reply
Re: [perlman] help with a script In reply to
the;

my($msg) = @_;

part may work....but I tend to use;

my $error = shift;

Not sure if one or the other is faster....I suppose its my coding practice that I've been brought up to. Also, there is no need to surround a my in brackets, unless you are defining more than one variable. i.e the following would work;

my ($var1, $var2, $var3);

or

my $var

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
Quote:
I suppose its my coding practice that I've been brought up to.

Wink

Quote:
Also, there is no need to surround a my in brackets, unless you are defining more than one variable. i.e the following would work;

Not true. There is a difference between my ($var) and my $var....try this Tongue

Code:
#!/usr/bin/perl

use strict;
main(qw/1 2 3/);

sub main {
my $foo = @_;
my ($bar) = @_;
print "Content-type: text/html\n\n";
print $foo;
print $bar;
}

See if you can work out why :)

Last edited by:

Paul: Feb 17, 2003, 8:57 AM
Quote Reply
Re: [Paul] help with a script In reply to
Mmm...interesting. Prints out 31 (or 3 and 1). I'm stumped as to why it does it though Unsure

Suppose we all learn something new every day :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
You all made me confused now.
I tried first right after posting last reply to get the script to work. Used my $msg = shift; as you wrote.
Tried the script, but it didn't work!
Now you tell me to use another one: my $error ?
Will this make a difference anyway. My point is that the script aint working!

Execution of /home/p/pt/pt-home/public_html/mypage/games_page/addgame.cgi aborted due to compilation errors.

A bit confused again? Will it work if I change the "my" part? And you said earlier that I didn't see the part where it said about all the errors:

Quote:
So you didn't see the list of errors, like;


--------------------------------------------------------------------------------
Quote
--------------------------------------------------------------------------------


syntax error at test.cgi line 130, near ") &&"
syntax error at test.cgi line 191, near ""<h2>Error</h2>\n":"
Missing right curly or square bracket at test.cgi line 196, at end of line
Execution of test.cgi aborted due to compilation errors.

No! I didn't! I don't have access to the errorlog and the only thing I see is: Execution of /home/p/pt/pt-home/public_html/mypage/games_page/addgame.cgi aborted due to compilation errors.

(and sorry for tearing up the silence in this thread again..Unsure)

Last edited by:

brewt: May 12, 2011, 12:18 PM
Quote Reply
Re: [perlman] help with a script In reply to
Its most likely down to the { } in the HTML, starting at line 8. Instead of using <<<FORMfields, try using;

print qq| and |; to close it.

The reason I think this is the problem, is cos the error I'm getting when testing it is;

Quote:
Missing right curly or square bracket at test.cgi line 199, at end of line
syntax error at test.cgi line 199, at EOF
Execution of test.cgi aborted due to compilation errors.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [perlman] help with a script In reply to
If you don't have access to the error log then theres not much anyone can say. We can view the code and correct any obvious errors but aside from that it's impossible to know what the error is.

You'll have to ask your host for access to the relevant part of the log.

Or at the top of your code add:

Code:
BEGIN {
open STDERR, ">error.log" or die $!;
};

...and then your errors will be directed to a file called error.log in the same directory as your script.

Last edited by:

Paul: Feb 17, 2003, 10:26 AM
Quote Reply
Re: [perlman] help with a script In reply to
BTW: Your error is in the &parse sub. You need to add an extra } bracket after the last one. The current one closes off the foreach $pair (@pairs) { loop.

That should sort it out Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] help with a script In reply to
Didn't work with the error log record. Permission denied it said. Im not shocked.

Changed the print <<FORMfields; to print qq|
and FORMfields to |;
Same error.

Then I added use strict;
Almost the same: addgame.cgi has to many errors.
Not the same message, but it didn't help much.
Unsure

I might soon listen to your advice and just create the whole thing over again, but ehhhh....dont want to! Unimpressed
Quote Reply
Re: [perlman] help with a script In reply to
Did you not see my reply about the fix? (just above your last one) I just tried it on my server, and it worked fine Tongue

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] help with a script In reply to
sorry. didn't see it. you wrote it some min. before me. sorry again.
and thanks. hope it will work! Smile
Quote Reply
Re: [perlman] help with a script In reply to
Get yourself a good debugger.
There is a realy good one here -
http://open-perl-ide.sourceforge.net/
then you won't have to worry error logs, you can develope and debug your stuff offline.

Bob
http://totallyfreeads.com.au