http vars

Jim Bowlin bowlin at sirius.com
Fri Jul 28 15:01:03 PDT 2000


Zolla Michalak wrote:
> 
> This may be a stupid question. Let me know if I'm making this more
> difficult than it needs to be.
> 
> Can someone please tell me how mapserver is able to grab name-value pairs
> in the HTML form with the same name and different values (i.e. name=layer
> value=snotel, name=layer value=rtg) without overwriting the first value
> with the second value? If you can tell me which file the code is in, I'll
> go look it up.

You can always use CGI.pm for this, but it is slow to compile.  Here is the
code that I use for this:

#--- get_full_query() ---------------------------------------------------
# Returns a structure from which all named parameters (including repeats)
# can be gotten with the param() routine.  
#------------------------------------------------------------------------

sub get_full_query {
    my $self = shift;

    my $str = "";
    $ENV{CONTENT_LENGTH} and read(STDIN, $str, $ENV{CONTENT_LENGTH});
    $ENV{QUERY_STRING} and $str .=  $ENV{QUERY_STRING};

    my $query;
    for (split /&/, $str) {
        my ($name, $value) =  map $self->url_decode($_), split /=/, $_;
        push @{$$query{$name}}, $value;
    }
    $self->{_query} = $query;
}

#--- url_decode($part) --------------------------------------------------
# Returns $part with the URL escape sequences decoded.  Be sure to
# make use of the raw "=" and "&" before using this routine otherwise
# important information will be lost.
#------------------------------------------------------------------------

sub url_decode {
    my $self = shift;
    my $part = shift;
    $part =~ tr/+/ /;
    $part =~ s/%([0-9A-Fa-f]{2})/pack("c", hex($1))/ges;
    $part;
}

#--- param($name) -------------------------------------------------------
# Gets named parameters stored in a structure created by get_full_query().
# Without $name returns list of parameter names or a reference to this
# list in scalar context.  If $name is defined, returns full list of all
# parameters associated with  $name or the first parameter associated with
# $name depending on context.
#------------------------------------------------------------------------

sub param {
    my ($self, $name) = @_;
    defined($name) or return wantarray ? keys %{$self->{_query}} : [keys %{$self->{_query}}];
    my $array = $self->{_query}{$name} or return;
    wantarray ? @$array : $$array[0];
}

__END__



More information about the MapServer-users mailing list