View Single Post
  #4 (permalink)  
Old 12-28-03, 10:39 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
See if this helps

Hi VisualActive,

I think I get what you're after now. See if this helps:

Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI::Carp qw/fatalsToBrowser/;

sub br_queries  {
  my ($string, $dat);
  if ($ENV{'REQUEST_METHOD'} eq 'GET') { $string = $ENV{'QUERY_STRING'}; }
  else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }

  # Regexes are expensive (CPU wise), do it as little as possible.  I see no
  # need to do it on every interation in your loop below.
  $string =~ s/\+/ /g;
  $string =~ s/\// /g;
  $string =~ s/\\/ /g;
  $string =~ s/%00//g;
  $string =~ s/%0a/newline/g;
  $string =~ s/(?:(\?)|&|[&;])s=[\w\d]{16,32}(?:&|&|;|$)//g;
  $string =~ s/%(..)/pack("c", hex($1))/ge;
  
  # If you are trying to put it in a hashref instead of a hash, do it from
  # the get-go.  Don't put it into a hash then iterate over it again to put
  # it in a hashref.
  foreach my $pair (split /&|;/, $string) {
    # It's much more readable if you use varialbe names here for your
    # keys and values
    my ($key, $value) = split /=/, $pair;
    if(defined $dat->{$key}) {
      $dat->{$key} .= "\0", $value;
    }
    else {
      $dat->{$key} = $value;
    }
  }
  return $dat;
}

# A quick test to see if it works
my $IN = br_queries;

print "Content-Type: Text/HTML\n\n";
print "<pre>\n";
print "Query string:\n";
print "-------------\n";
print "Key: $_ - Value: $IN->{$_}\n" for keys %$IN;
print "-------------\n";
print "</pre>\n";
I still highly reccomend that you look into using the CGI.pm module I linked to above. Unless you are required to do this for a school project the CGI module is a tried and tested system for doing this and is a standard perl module.

~Charlie
Reply With Quote