Forum Moderators: coopster & phranque

Message Too Old, No Replies

Simple links listing gone awry

         

grandpa

5:37 am on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I found a nice little directory processing script, and from a logic view I understand most if not all of it. But not being a 'script person' some of the finer points are being missed - and I have no doubt they are important.

Here's what's happening, and not happening. The script reads a file, 1 record per line, fields are delimited with a pipe. I call the script and pass a parameter from my html with <path to cgi-bin>script.cgi. So far, everything is ok. The parameter starts the script in the right place.
Since it's processing my directory listings, these are defined like this:

%Script = (
'arts','/home/myweb/public_html/drs/arts',
'business','/home/myweb/public_html/drs/business',
'computers','/home/myweb/public_html/drs/computers',
);

Then one of these has to be selected as the default:
$script = 'arts';

So the script runs, displays my document as expected. But no matter what category is selected, the default is always processed. For example, if I check Business, no results are returned - and I suspect its because of the default (and some flaw in the logic).

I can force results by taking these actions:

In the URL bar, this line appears *after* returning the first set of results
***p://wwWebmasterWorldeb.com/cgi-bin/directory.cgi?script=arts

If I manually change the line to
1) ***p://wwWebmasterWorldeb.com/cgi-bin/directory.cgi?script=business
2) press GO
3) and then select the Business category, I will see the results I expect.

I'm hoping that by posting symptoms instead of code I can be led down the path of understanding. It appears that the code is not processing the script=? properly. It looks like it wants to return results, but the category is incorrect - it's remaining the default no matter what category is selected.

After writing this I think I at least understand the problem a little more clearly. So I'm going to post this one little snippet of code in the hope that this is where my problem lies. I'm guessing this is what processes the checkbox selections when I submit the query.

@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{ ($name,$value) = split(/=/,$cgiPair);
$name =~ s/\+/ /g; $value =~ s/\+/ /g;
$name =~ s/%(..)/pack("c",hex($1))/ge;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$Form{$name} .= "\0" if (defined($Form{$name}));
$Form{$name} .= "$value";
if ($name =~ /start(\d+).*/) { $Form{'start'} = $1; }

Thanks
grandpa

grandpa

11:37 am on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A follow up: I would be grateful is someone could put that little code snippet into common language. I realize that unless there is some really obvious error there I probably won't get much feedback. So just knowing what that code means will help understand and debug this script.

Thanks
grandpa

claus

12:38 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The snippet (except for the last three lines) is a standard form input handling routine.
It seems to me you are missing an } but i assume it is below your quote.

@cgiPairs = split(/&/,$buffer);

Make an array called cgiPairs and store values here. Values are separated by ampersands ("&")

foreach $cgiPair (@cgiPairs)

Start a loop through each of the value-name pair strings in the array

{ ($name,$value) = split(/=/,$cgiPair);

Loop starts with { ends with the missing } - split pair strings into the parts "name" and "value" using equal sign as delimiter

$name =~ s/\+/ /g; $value =~ s/\+/ /g;
$name =~ s/%(..)/pack("c",hex($1))/ge;
$value =~ s/%(..)/pack("c",hex($1))/ge;

Translate plusses to spaces and translate hex-encoded (url-encoded) characters to real characters. The above lines are not likely the cause of the error, they're just collecting the names and values, and making the text look like it should.

$Form{$name} .= "\0" if (defined($Form{$name}));

Add "\0" to the end of the names, if they are defined.

$Form{$name} .= "$value";

Add the value to the name or to the above. No matter if the names are defined or not, just do it.

if ($name =~ /start(\d+).*/) { $Form{'start'} = $1; }

Regular expression test: if the name is "start" followed by one or more digits, followed by any number of any type of characters

...then give "start" a new value, which is $1. Now, what is "$1"?

$1 is whatever was catched in the first parenthesis of the previous regular expression. Which seems to be "one or more digits/numbers".

--
I do hope this gave you a clue, because i'm not personally able to tell if any of this should be the reason your script is not working as it should.

Also, i hope i didn't make too many errors, others will no doubt be able to correct me if i did. Don't think so, though ;)

/claus

alexhudson

9:08 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



Hi Grandpa

If you could post the checkbox HTML, it might give us a clearer idea. I don't really understand what is failing - you're selecting the category from checkboxes or something? If that's not selecting the right category, then the problem is an HTML problem, not a Perl problem.

In terms of making that piece of rubbish code ;) actual genuine Perl, what it seems to be trying to do is parse CGI data. Not using the Perl CGI module for this is always wrong.

As an example piece of code, if you wanted to set $script to the CGI value of script (i.e., passed through the URL) you would do:


use CGI;

my $cgi = new CGI; # grab a CGI object, this is OO style
my $script = $cgi->param ('script');

print $cgi->header ('text/plain');
print<<_END_;
The value of the script parameter: $script
_END_

As simple as that, really. Perl doesn't need to be hard unless you make it hard.

grandpa

11:21 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



claus:

Thank you. Yes, the missing "}" is there, I just missed it in my pasting. The script isn't failing, but I bet I would see any number of errors if that were missing.

I think I can reverse engineer this puppy now that I have a better understanding of what it's trying to do....

alexhudson:

I can't speak to the quality of the code - it may well be rubbish :) But when I'm trying to get a particular task done, and my budget is zero, I've got to take what I can find and work with it. And hopefully, learn something from it too. (Oh No! Another hack coder is being born :) )

Yes, the category is selected from checkboxes. The script builds the table in my html. It appears to be accepting the checked category, but no results are displayed unless the default category (coded in the script) and the selected category are the same.

As simple as that, really. Perl doesn't need to be hard unless you make it hard.

It's not so much that it's hard, I don't understand the syntax used... yet. For example, the line you posted:

print<<_END_;

I can figure out it's displaying the html page footer
(I think).

Aside: If I ever decide to help anyone else with their website, the pre-requisites will be 1) a good library of reference material and 2) a subscription to WW.