Gossamer Forum
Home : General : Perl Programming :

Error spotter needed...

Quote Reply
Error spotter needed...
Can anyone see the error in this code - I've been racking my brain for hours and can't see it.

perl -c script.cgi produces "Syntax OK" but when I run it I get a 500.

Code:
sub main {
Code:
my $dir = "/home/sites/www.domain.com/web/search";
my $username = $IN->param('username');
my $password = $IN->param('password');
Code:
opendir(DIR,"$dir/") || &cgierr("Could not open $dir/ : $!");
@files = grep { /\.txt$/, readdir(DIR); }
closedir(DIR);
Code:
foreach (@files) {
if ("$username.txt" eq $_) {
&site_html_track_failure("Sorry, this username exists.") and return;
}
else {
open(FILE,">$dir/$username.txt") || &cgierr("Could not open $dir/$username.txt : $!");
print FILE "$username|$password\n";
close(FILE);

&site_html_track_login("Thankyou, you may now login below.") and return;
}
}

}
Help is appreciated - thanks.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
Hi Paul,

Are you printing out a header anywhere? Can you run it from a shell and see if you get any errors?

Regards
~Charlie

Quote Reply
Re: Error spotter needed... In reply to
Running it from telnet tells me the script can't be run from telnet...lol

I am printing the headers in site_html_templates.pl as the template is loaded.

Just let me check....

Yep I have &html_print_headers; in the sub that is loading the templates.



Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
Hmm...
Is $username|$password\n making it into the file or is it blowing up before it makes it that far? Are you also printing a header in &cgierr? I have a really bad habbit of not printing my headers and not being able to figure out why I keep getting a 500 :) Does your foreach loop only loop one time? It looks to me like it only checks the first $_ and then exits.

~Charlie

Quote Reply
Re: Error spotter needed... In reply to
No the file isn't even being created.


I tried adding &cgierr("Grep booboo : $!"); to the @files = grep... line and that seemed to be causing the problem as cgierr() was called a few times but I tweaked a few things here and there and now it doesn't call cgierr() and just dies.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
I think I know what it is....the tracker/ directory is currently empty, therefore @files will be empty so the foreach loop is dying because there is nothing for it to loop though. Plausible?

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
Doh!

That's what it was.

When I surrounded the foreach loop with
Code:
if ($#files > -1) {

CODE

} else {
&cgierr("Nothing in $dir/");
}
cgierr() was called.

Thanks for your help - you got my brain working again!

Smile

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
This might work:



<EDIT>NM, you got it working the code I posted was very wrong.</EDIT>

~Charlie
Quote Reply
Re: Error spotter needed... In reply to
Hi thanks for that code sample though.

Here is what I ended up with:

Code:
sub main {

my $dir = "/home/sites/www.cyberwhispers.com/web/search/tracker";
my $username = $IN->param('username');
my $password = $IN->param('password');


if (-e "$dir/$username.txt") {
&site_html_track_failure("Sorry, the username *$username* exists.") and return;
}
else {
open(FILE,">$dir/$username.txt") || &cgierr("Could not open $dir/$username.txt : $!");
print FILE "$username|$password\n";
close(FILE);

&site_html_track_login() and return;
}
}
Much easier than using grep.
Thanks again.



Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Error spotter needed... In reply to
Is that your new project?
Quote Reply
Re: Error spotter needed... In reply to
Hi,

It is a non-cookie "MyLinks" mod that I am writing for a site that I am co-webmaster of.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/