in reply to Re: Re: Re: use CGI
in thread making a hash from cgi parameters

I am trying to do it with both the URL as well as POST.. but if you are using POST.. in order to get that, you have to use STDIN so if i sent something from a form method=POST.. in my script i have to get that value from STDIN

Replies are listed 'Best First'.
(Ovid - grabs params separately and merge them)Re(5): use CGI
by Ovid (Cardinal) on May 25, 2001 at 01:50 UTC

    Here's an easy way to deal with that.

    my %post_data = map { $_ => param( $_ ) } param; my %url_data = map { $_ => url_param( $_) } url_param; my %data = merge( \%post_data, \%url_data ); sub merge { my %merged; for (@_) { while (my ($key, $value) = each %$_) { push @{$merged{$key}}, $value; } } %merged; }

    %data will then contain all key value pairs. Note, this is not the best way to do this, but it's fairly easy to understand (though you'll want to read about map and references to grok all of it). Further, all values will be list references with this method, so accessing a single 'color' parameter would be $data{'color'}->[0]. The other option is to leave the two hashes separate and use them as necessary, but then you again have two data structures for one type of data. Yuck.

    Oh, and the &merge is from MeowChow :)

    As a side note: you shouldn't be mixing GET and POST. They have different uses. GET is for getting info and POST is for posting info. GET requests are often cached and POST requests shouldn't be. Mixing these methods could have undesireable side-effects.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Re: Re: Re: Re: use CGI
by swiftone (Curate) on May 25, 2001 at 02:03 UTC
    Ah. I thought CGI.pm handled that, but I see it does not. Here's an alternate to Ovid's offering that doesn't try to keep the two options seperate. Note this probably isn't the best way to do it either, but this does work:
    #!/usr/bin/perl -w use strict; use CGI; use URI::Escape; my $query = new CGI; my $plus; $plus .= "$_=".uri_escape($query->param($_)).";" foreach ($query->para +m()); if($ENV{REQUEST_URI}=~/^[^?]*\?(.*)/){ $plus.=$1; } $query = new CGI ($plus); my %data = $query->Vars(); print $query->header; #print HTTP header while (my ($key, $value) = each %data){ print "<br>$key--$value\n"; } print qq( <form method=post action=test.cgi?test=foo> <input name=bar> <input type=submit> </form> );
    Update: use url_param(), I was looking at old docs and missed it.