Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003
    Posts
    2

    Unanswered: QUERY_STRING parsing in Perl

    Could someone help how to parse $QUERY_STRING parameters in Perl into Perl variables?
    For example I have url?name=john&surname=jameson
    How to get "john", "jameson" into variables?

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Code:
    @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;
    	if($INFO{$v} ne ""){
    		$INFO{$v} .= ",$i";
    	}else{
    		$INFO{$v} = $i;
    	}
    }
    Now you have all your requested information in the %INFO-hash.
    Code:
    print $INFO{name}; # ouput: john
    print $INFO{surname}; # output: jameson
    but I would recommend to use the CGI-module
    Code:
    use CGI;
    my $cgi = CGI->new();
    my %params = $cgi->Vars();
    print $params{name};# ouput: john
    print $params{surname};# output: jameson
    board.perl-community.de - The German Perl-Community

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •