how do I retreive query string in perl ?

let's say I have http://server.com?name=foo
how do I get the value of 'name'

thx

cvv3@yahoo.com

Recommended Answers

All 8 Replies

use CGI;

chomp($name = param("name")); # // $name should have "foo" in it.

nevermind this post. The thread didn't show me there were any replies on the page before I responded.

$str="http://server.com?name=foo&age=20&id=3&pass=123456";
$str=~s/^.*?\?//;
while($str=~m/(.*?)\b=(.*?)(&|$)/){
	print "$1----$2\n";
	$str=$';
}

how will that parse the query string? That parses a string. There is no need to do something like that anyway nor is it advisable, use the CGI module or a CGI sub class to parse query or form data.

$qs=$ENV(QUERY_STRING);

oops... I meant:

$qs=$ENV{QUERY_STRING};

Since it is a hash.

I would suggest using the CGI module as well, but if you really want to do it by hand... Here is an extremely messy, but effective way of parsing (not strict BTW):

############
sub getcgi {
############
	read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $input);
	foreach $pair(@pairs) {

	        ($name, $value) = split(/=/, $pair);
	        $name =~ tr/+/ /;
	        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $value =~ tr/+/ /;
	        
	        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
					#$value =~ s/< *(\/*)[^>]*script[^>]*>/<\1scrypt>/ig;
					#$value =~ s/< *(\/*)[^>]*iframe([^>]*)>/\*\1yframe\2\*/ig;
	        $value=~tr/\'/\`/;
	        $input{$name} = $value;
	}
	@vars = split(/&/, $ENV{QUERY_STRING});
	foreach $var(@vars) {
	        ($v,$i) = split(/=/, $var);
	        $v =~ tr/+/ /;
	        $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ tr/+/ /;
	        $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ s/<!--(.|\n)*-->//g;
	        $v =~ tr/\'/\`/;
	        $info{$v} = $i;
	}

	@vars = split(/;\s?/, $ENV{HTTP_COOKIE});
	foreach $var(@vars) {
	        ($v,$i) = split(/=/, $var);
	        $v =~ tr/+/ /;
	        $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ tr/+/ /;
	        $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ s/<!--(.|\n)*-->//g;
	        $v =~ tr/\'/\`/;
	        $cookie{$v} = $i;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.