Gossamer Forum
Home : General : Perl Programming :

Error spotting (again)

Quote Reply
Error spotting (again)
Can anyone spot the error in this code?...I've been looking and changing things around for about an hour and can't figure out why it won't work.

Basically I have a text file with this in:

dsfsbdvs8fu8707|Name_of_something_here

Now, in my script I have:
Code:
sub get {

my $code = $in->param('code');

open(FILE,"</path/to/codes.txt");
@data=<FILE>;
close(FILE);

foreach (@data) {
chomp $_;
($un,$dl) = split(/\|/, $_);
if ($code eq $un) {
Do something with Name_of_something_here
} else {
&error("Invalid code : $code");
}

}
For some reason I always get the error page, even if I go to the following URL:

http://www.wiredon.net/gt/script.cgi?code=dsfsbdvs8fu8707

Any ideas?.....I'm sure it is a simple mistake but even printing things out to debug didn't help because everything looked as it should. I even compared the value of the query string with the value of $un that I printed out and they were identical. Maybe there is a \s or \n somewhere but I thought chomping would sort that out?

Thanks.

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

Quote Reply
Re: Error spotting (again) In reply to
I get a CGI WRAPPER ERROR:

In Reply To:

Execution of (/home/sites/site13/web/gt/script.cgi) is not permitted for the
following reason:

Script file not found.


Regards,

Eliot Lee
Quote Reply
Re: Error spotting (again) In reply to
Sorry that's my fault.

Script.cgi was an example and not the real name of the script.

In the text file at the moment I have:

sliWluaP115990546401476|ReportIt.zip

So if you go to:

http://www.wiredon.net/...WluaP115990546401476

...then ReportIt.zip should be downloaded, but it keeps going to the error page Frown


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

Quote Reply
Re: Error spotting (again) In reply to
Shouldn't the following code:

Code:

if ($code eq $un) {


BE

Code:

if ($code eq $dl) {


Regards,

Eliot Lee
Quote Reply
Re: Error spotting (again) In reply to
I am comparing the code in the query string to the code in the database, not the file name.

I think if I used your example it would be trying to compare

sliWluaP115990546401476 against ReportIt.zip

Wouldn't it?


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

Quote Reply
Re: Error spotting (again) In reply to
Sorry...okay...I see now...hmm...if I think of something, I'll post it later...

bye..

Regards,

Eliot Lee
Quote Reply
Re: Error spotting (again) In reply to
ok no worries.

Thanks


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

Quote Reply
Re: Error spotting (again) In reply to
Hi Paul:

The code would invoke the error if the data string was not the first entry in the data file. Is there just the one line in the data file?


Dan Cool

LotusLand
Vancouver, BC, Canada
Top Ranked City in World for Quality of Life
Quote Reply
Re: Error spotting (again) In reply to
No there are 4 lines.

I thought foreach looped through the whole file?

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

Quote Reply
Re: Error spotting (again) In reply to
So would using while be better?

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

Quote Reply
Re: Error spotting (again) In reply to
Hi Paul:

The loop exits (to error sub) with the first $un that does not equal $code - which will be premature if not first entry in data file. The loop could continue if the error sub did not exit but that would be silly. One solution is:

http://www.gossamer-threads.com/...&vc=1#Post137257

Another (less ideal) solution is to maintain what you got now and use flag: if found, set flag, and then deal with flag outside loop. For example, if found, $found = 1 and then error if ! $flag.


Dan Cool

LotusLand
Vancouver, BC, Canada
Top Ranked City in World for Quality of Life
Quote Reply
Re: Error spotting (again) In reply to
Paul, you're exiting the loop after the first test.

try...
Code:
my $found;
open (DATA, "$datafile") or die $!;
while (<DATA>)
chomp;
my ($un,$dl) = split(/\|/, $_);
($code eq $un) or next;
# (do stuff here)
$found++;
}
close (DATA);
$found or error("Invalid code: $code.");
--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com
Quote Reply
Re: Error spotting (again) In reply to
Thanks Junko/Dan,

My one perl book needs a few friends I think...

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

Quote Reply
Re: Error spotting (again) In reply to
for future reference, use CGI::Carp 'fatalsToBrowser' or tail your web sever logs and add error checking code to your scripts. it will save you and everyone here a lot of time.
there are a million ways to make a script error. combing through it line by line is the masochistic way to solve bugs. proper error checking will save you every time.

Code:
sub get {
my $code = $in->param('code');

# add an 'or die' with an error message containing $!
# no need for the '<' operator. it's the default
# add the file path to a variable for scalability
open(FILE,"</path/to/codes.txt");

# don't slurp the file. it's bad form and often harmful
@data=<FILE>;
close(FILE);
foreach (@data) {

# chomp defaults to $_ so just use 'chomp;'
chomp $_;

# bad scoping
($un,$dl) = split(/\|/, $_);
-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Error spotting (again) In reply to
It does have error checking but I removed it before I posted. I always use $! or pass errors to an error sub or use CGI::Carp qw(fatalsToBrowser); but just missed them out in my post.

I guess I was asking for a hammering....

Using CGI::Carp qw(fatalsToBrowser); doesn't always seem to work. Sometimes it still gives a 500. Is there a reason for that?

What's bad scoping?

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

Quote Reply
Re: Error spotting (again) In reply to
a variable's scope is where it is located in the script's object hierarchy.
you should always use 'my' to scope your variables and have as few global vars as possible. aside from paths and system vars etc you should have all variables scoped directly to their subroutine (or loop/if statement within the sub).

always 'use strict'. even when learning. it will force you to scope your vars correctly for one. once you get comfortable doing any task with
#!/usr/bin/perl -w
use strict;

start learning the ins and outs of mod_perl.
once you can build a full-scale web app in mod_perl (to answer your earlier question) you are a programmer.

and you can tell all those JSP tools what a real web lang is;)

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';