lpoht has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I'm new to perl, but can't seem to find an answer to my problem. I have a piece of code which grabs HTTP query string variables as such:
$pageid=$query->param('pageid');
where $query is the query string. I want to be able to ignore the case of the names of the parameters (i.e. accept "PageID", "PagEiD", etc.) without grabbing the whole query string (with the ENV variable), then splitting it, searching it, etc. Any ideas? Thanks.
Re: Ignore case of query string parameter
by George_Sherston (Vicar) on Jun 12, 2002 at 18:55 UTC
|
Other monks may have a specific way to do what you want, but you might also find it useful that CGI.pm (I think I'm right in guessing you are using this?) offers:
@array_of_submitted_parameter_names = $query->param;
So you could get them all and then loop through them picking up the ones that meet your needs, e.g. in the case you're looking at:
my @ary = $query->param;
my $pageid;
for (@ary) {
if (lc $_ eq 'pageid') {
$pageid = $query->param($_);
}
}
§ George Sherston | [reply] [d/l] [select] |
Re: Ignore case of query string parameter
by c-era (Curate) on Jun 12, 2002 at 18:59 UTC
|
I haven't tested this code, but it should work ;)
my %params;
for my $paramName ($query->param){
$params{lc($paramName)} = $query->param($paramName);
}
All of your params should be in the %params hash, and you just use the lc version of your param name to access your value. | [reply] [d/l] |
|
same untested code but written into one line :)
my %params = map {(lc($_)=>$query->param($_))} $query->param;
(personally I tend to write similar code this way)
Best wishes
Weasel | [reply] [d/l] |
|