in reply to Re: Re: Re: Preventing GET and POST from getting mixed-up
in thread Preventing GET and POST from getting mixed-up

I'd appreciate it if someone would know a better way

Well, it seems like I've had to do it the way I thought I'd have to do it. So here is my code, make of it what you will (feedback welcome). One caveat... all this code is copied out of an object which is currently too messy to post. In order to work you'll need to have $self->{'request'} = Apache->request; somewhere in your initialisation code.

# @foo = get_param() # returns the parameter names (or undef if none) # $foo = get_param("param") # returns the named parameter (or undef if not) # @foo = get_param("param") # as above in list context # ================================================== sub get_param { my $self = shift; my $param = shift || undef; unless (exists $self->{get_vars}) { # Set to undef if no args unless ($self->{'request'}->args) { warn "I see no GET vars!\n" if $DEBUG; return $self->{'get_vars'} = undef; } # Now run the query_string through the parser $self->_parse('get_vars', scalar($self->{'request'}->args) +); } return undef unless defined $self->{'get_vars'}; return keys %{$self->{'get_vars'}} unless $param; return undef unless exists($self->{'get_vars'}->{$param}); return $self->{'get_vars'}->{$param} unless ref $self->{'get_vars' +}->{$param}; return wantarray ? @{ $self->{'get_vars'}->{$param} } : "@{ $self- +>{'get_vars'}->{$param} }"; return undef; ## Inconceivable! } # ================================================== sub post_param { my $self = shift; my $param = shift || undef; unless (exists $self->{'post_vars'}) { # Set to undef if not POST unless ($self->{'request'}->method_number == M_POST) { warn "I see no POST vars\n" if $DEBUG; return $self->{'post_vars'} = undef; } # Now run the query_string through the parser $self->_parse('post_vars', scalar($self->{'request'}->cont +ent)); } return undef unless defined $self->{'post_vars'}; return keys %{$self->{'post_vars'}} unless $param; return undef unless exists($self->{'post_vars'}->{$param}); return $self->{'post_vars'}->{$param} unless ref $self->{'post_var +s'}->{$param}; return wantarray ? @{ $self->{'post_vars'}->{$param} } : "@{ $self +->{'post_vars'}->{$param} }"; return undef; ## Inconceivable! } # ================================================== sub _parse { my $self = shift; my $dst = shift; my $str = shift; warn "_parse: $dst $str\n" if $DEBUG; foreach my $pair (split /&/, $str) { $pair =~ tr/+/ /; $pair =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi; my( $name, $value ) = split /=/, $pair; $value = $value || ""; if ( exists ($self->{$dst}->{$name}) ) { if ( ref $self->{$dst}->{$name} ) { push @{ $self->{$dst}->{$name} }, $value; } else { $self->{$dst}->{$name} = [ $self->{$dst}->{$name}, $va +lue ]; } } else { $self->{$dst}->{$name} = $value; } } }
--
TTFN, FNORD

xaphod