954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

retreive query string

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

[email]cvv3@yahoo.com[/email]

perl1user
Newbie Poster
8 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 
use CGI;

chomp($name = param("name")); # // $name should have "foo" in it.
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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

[email]cvv3@yahoo.com[/email]

Already answered on one or two other forums.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
$str="http://server.com?name=foo&age=20&id=3&pass=123456";
$str=~s/^.*?\?//;
while($str=~m/(.*?)\b=(.*?)(&|$)/){
	print "$1----$2\n";
	$str=$';
}
summer_cherry
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 
$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.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
$qs=$ENV(QUERY_STRING);
mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

oops... I meant:

$qs=$ENV{QUERY_STRING};


Since it is a hash.

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

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;
	}
}
mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You