Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL query handling

Status
Not open for further replies.

Tama

MIS
Jun 6, 2001
121
0
0
NZ
I have a script which can be called with a couple of options in the URL query field of the script (after the "?") so there's a couple of lines in the script with along these lines...

Code:
elsif ($ENV{'QUERY_STRING'} eq "average") {&average;}

Which checks the query, and if the url is myscript.cgi?average then it runs the average subroutine.

Now - I want to change the script so the query line can be something along the lines of myscript.cgi?average&s=6&f=10 - and my subroutine will still run, and I can get the values of s and f

I'm interested to find out what is the best/cleanest way of carrying this out.

Thanks in advance
 
first do it will be like this ?average=6&s=6&f=10
if u dont use cgi.pm or cgi-lib
u have a function like this :
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$FORM{$name} = $value;
}

no?
then do something like

&no_average_value unless $FORM{average};
if ($FORM{average} == 1 ) {
# do something
} elsif ($FORM{average} == 2 ) {
# etc...


hope this help someone knowledge ends where
someone else knowledge starts
 
see if this might help you.

#!/usr/bin/perl -w

use strict;
use CGI;

my $cgi = new CGI();

print $cgi->header;

my %HashOfFunctions = (
'add' => sub {
my $first = shift;
my $second = shift;

return($first + $second);
},

'average' => sub {
print &quot;inside HashOfFunctions<br>&quot;;
my $param1 = shift;
my $param2 = shift;

print &quot;param1 : $param1<br>&quot;;
print &quot;param2 : $param2<br>&quot;;
}
);

my($function, $one, $two) = split /&/,$ENV{'QUERY_STRING'};


print <<HTML;

qs = $ENV{'QUERY_STRING'}<br>

function = $function<br>

one = $one<br>

two = $two<br>


HTML


$HashOfFunctions{$function}->($one, $two);
 
Cheers for that...

...if I stare really hard and bag my head with something solid I think I can work out what the scripts are doing. I'm a bit of a lemon with Perl.

Will have a play around tomorrow night, and see how it goes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top