943,600 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 4246
  • Perl RSS
Jan 10th, 2009
0

retreive query string

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
perl1user is offline Offline
8 posts
since Jan 2009
Jan 10th, 2009
0

Re: retreive query string

perl Syntax (Toggle Plain Text)
  1. use CGI;
  2.  
  3. chomp($name = param("name")); # // $name should have "foo" in it.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 11th, 2009
0

Re: retreive query string

Click to Expand / Collapse  Quote originally posted by perl1user ...
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
Already answered on one or two other forums.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jan 11th, 2009
0

Re: retreive query string

Perl Syntax (Toggle Plain Text)
  1. $str="http://server.com?name=foo&age=20&id=3&pass=123456";
  2. $str=~s/^.*?\?//;
  3. while($str=~m/(.*?)\b=(.*?)(&|$)/){
  4. print "$1----$2\n";
  5. $str=$';
  6. }
  7.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
summer_cherry is offline Offline
1 posts
since Jan 2009
Jan 15th, 2009
0

Re: retreive query string

nevermind this post. The thread didn't show me there were any replies on the page before I responded.
Last edited by Phaelax; Jan 15th, 2009 at 8:03 pm.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 15th, 2009
0

Re: retreive query string

Perl Syntax (Toggle Plain Text)
  1. $str="http://server.com?name=foo&age=20&id=3&pass=123456";
  2. $str=~s/^.*?\?//;
  3. while($str=~m/(.*?)\b=(.*?)(&|$)/){
  4. print "$1----$2\n";
  5. $str=$';
  6. }
  7.  
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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Feb 5th, 2009
0

Re: retreive query string

Perl Syntax (Toggle Plain Text)
  1. $qs=$ENV(QUERY_STRING);
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Feb 5th, 2009
0

Re: retreive query string

oops... I meant:

Perl Syntax (Toggle Plain Text)
  1. $qs=$ENV{QUERY_STRING};

Since it is a hash.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Feb 5th, 2009
0

Re: retreive query string

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):

Perl Syntax (Toggle Plain Text)
  1. ############
  2. sub getcgi {
  3. ############
  4. read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
  5. @pairs = split(/&/, $input);
  6. foreach $pair(@pairs) {
  7.  
  8. ($name, $value) = split(/=/, $pair);
  9. $name =~ tr/+/ /;
  10. $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  11. $value =~ tr/+/ /;
  12.  
  13. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  14. #$value =~ s/< *(\/*)[^>]*script[^>]*>/<\1scrypt>/ig;
  15. #$value =~ s/< *(\/*)[^>]*iframe([^>]*)>/\*\1yframe\2\*/ig;
  16. $value=~tr/\'/\`/;
  17. $input{$name} = $value;
  18. }
  19. @vars = split(/&/, $ENV{QUERY_STRING});
  20. foreach $var(@vars) {
  21. ($v,$i) = split(/=/, $var);
  22. $v =~ tr/+/ /;
  23. $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  24. $i =~ tr/+/ /;
  25. $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  26. $i =~ s/<!--(.|\n)*-->//g;
  27. $v =~ tr/\'/\`/;
  28. $info{$v} = $i;
  29. }
  30.  
  31. @vars = split(/;\s?/, $ENV{HTTP_COOKIE});
  32. foreach $var(@vars) {
  33. ($v,$i) = split(/=/, $var);
  34. $v =~ tr/+/ /;
  35. $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  36. $i =~ tr/+/ /;
  37. $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  38. $i =~ s/<!--(.|\n)*-->//g;
  39. $v =~ tr/\'/\`/;
  40. $cookie{$v} = $i;
  41. }
  42. }
  43.  
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: running perl
Next Thread in Perl Forum Timeline: perl project command line arg?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC