Perl and cgi help

software development

#1

I am new to this stuff and I am trying to count the number of downloads each of the games on my site gets. When someone clicks “download” they are sent to a .pl file that is supposed to open a data file, add 1 to the overal number of downloads, close the file, and then redirect them to the .zip file location.

Here is the code:

#!/usr/bin/perl

open(INF,“flash_info.dat”) or dienice(“Couldn’t wright data to file. $!”);

@flashinfo = ;

close(INF);

$ENV{‘QUERY_STRING’} =~ tr/ / /;
$cat = $ENV{‘QUERY_STRING’};

foreach $i (@flashinfo) {
chomp($i);
($fname,$user,$plays,$dloads,$type,$location,$wi,$hi,$dload,$zip) = split(/|/,$i);
if ($fname eq $cat) {
$stuff = $i;
}
}

foreach $i ($stuff) {
chomp($i);
($fname,$user,$plays,$dloads,$type,$location,$wi,$hi,$dload,$zip) = split(/|/,$i);
}

open(INF,">flash_info.dat") or dienice(“Couldn’t wright data to file. $!”);
flock(INF,2);
seek(INF,0,0);
$dloads_add = $dloads + 1;
foreach $i (@flashinfo) {
chomp($i);
if ($i eq $stuff) {
print INF “$fname|$user|$plays|$dloads_add|$type|$location|$wi|$hi|$dload|$zip\n”;
} else {
print INF “$i\n”;
}
}

close(INF);

print “Location:$zip\n\n”;

Every time I upload it ther is a Internal Server Error and The script won’t run. I am sure I am missing something very obvious.

Any help will be greatly appreciated.

Brad Campbell
woochoochinchilla.com


#2

Does anything appear in your site’s error log when the script runs?

Some obvious possibilities:

  1. Have you uploaded the script with Unix line endings?

  2. Are the permissions set correctly? See http://wiki.dreamhost.com/index.php/Perl#Why_Perl_CGIs_can_fail_mysteriously

  3. I can’t see dienice defined in your script. To see if that’s the problem, change dienice to die. (By the way, if the script can’t write to the file for some reason then it’ll just die and the user won’t be able to download your games. Is that the required behaviour?)

Cheers,
Ian


#3

Again I am new to this so bare with me. What do you meen by Unix line endings?

I know my permissions are set correctly because I chmoded it to 755 using the webftp.dreamhost.com.

I just tryed die and that too did not work.

Also my program is a .pl file… should I change it to .cgi or will that do nothing.

Brad Campbell
woochoochinchilla.com


#4

You shouldn’t need to change the script to .cgi.

The easiest way to solve this would be have a look at your error log and see what it says. Otherwise I’m just speculating.

Cheers,
Ian


#5

How do I check the error log?

Brad Campbell
woochoochinchilla.com


#6

It should be somewhere like:

logs/yourdomain.com/http/error.log

Cheers,
Ian


#7

My error log says “Premature end of script headers.”

What does that meen?

Brad Campbell
woochoochinchilla.com


#8

It could mean a few things. Often, it indicates that the line endings are wrong. That is, it may mean that you’ve got Windows or Mac line endings, but the server wants Unix line endings.

To see if this is the problem, try uploading a minimal test script, like this:

#!/usr/bin/perl

print “Content-type: text/html\n\n”;
print “OK”;

If that gives an error then it’s probably line endings, in which case you have a number of options:

  1. Change to Unix line endings in your text editor. What operating system are you using? And what text editor?

  2. Specify ASCII mode (not BINARY) when FTPing the file to DreamHost. This will do the conversion for you.

  3. If you’ve got shell access on DreamHost then run:

dos2unix your-file.pl

Cheers,
Ian


#9

Fixed!

Thanks Ian.

Brad Campbell
woochoochinchilla.com