Click to See Complete Forum and Search --> : Simple PERL issue.


Ultimater
02-23-2005, 10:16 PM
Hey, my PERL code is not working at all!
I'm running the game JS Falcon, when the user gets a high score,
I want the user to click my button to submit the highscore.
My PERL is supposed to read the value of the "SCORE"
and write it to a .JS file that is also located in my cgi-bin.

HERE'S the submiting layer:
(The DIV emerges via JavaScript when the Highscore is beaten.)

<DIV ID="sendLay" STYLE='position:absolute; left:48; top:-1000'>
<FORM NAME="HIS" method="post" action="../cgi-bin/falcon_hs.pl">
<INPUT NAME="SCORE" type="hidden" size="20">
<INPUT NAME="B1" type="submit" value="Send HIGH SCORE" onSubmit="reload(true)">
</FORM>
</DIV>


The URL of the above segment of my game is:

http://ultimiacian.tripod.com/games/JSFalcon.html (http://ultimiacian.tripod.com/games/JSFalcon.html)

Then I have a PERL file in the current URL:
http://ultimiacian.tripod.com/cgi-bin/falcon_hs.pl

The PERL code reads:

use ReadParse;
&ReadParse;
$jsfile="falcon_hs.js";
$in{'SCORE'} =~ s/[^0-9a-z]//g;

open(DAFILE, "<<$jsfile");
print DAFILE "SCORE=54000";
close(DAFILE);


The ReadParse.pm is:

sub ReadParse {
local (*in) = @_ if @_;
local ($i, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/[&;]/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}

return scalar(@in);
}
1

Ultimater
02-23-2005, 10:20 PM
I didn't write the ?> part above in my ReadParse.pm.
The 1 is the last line of that code.

Nedals
02-24-2005, 01:47 AM
My recomendation...
Get rid of that ReadParse module altogether and start using 'strict'.

use strict;
use CGI; ## which comes with Perl
my $q = new CGI;

## now read your form data with...
my $score = $q->param('score'); ## that's it

I'm not sure what you are doing here. Why include lowercase chars in a score?
$in{'SCORE'} =~ s/[^0-9a-z]//g;

Ultimater
02-24-2005, 01:55 PM
Originally posted by Nedals
I'm not sure what you are doing here. Why include lowercase chars in a score?
$in{'SCORE'} =~ s/[^0-9a-z]//g;
That one slipped by on me un-noticed. I copied
that statement from a different program.
I should have used
$in{'SCORE'} =~ s/[^0-9]//g;

But using your recommendation I'd use
$score =~ s/[^0-9]//g;
after I include everything else you wrote.

By the way, do PM files need to end with a return value?

Nedals
02-24-2005, 08:38 PM
...I'd use $score =~ s/[^0-9]//g; after I include everything else you wrote.Correct, if you want to remove non-numeric characters
By the way, do PM files need to end with a return value?They need to end with a true value, usually..

..code..
1;