Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

HTTP Variables In A Hash

by emcb (Beadle)
on Dec 30, 2001 at 08:13 UTC ( #135202=perlquestion: print w/ replies, xml ) Need Help??
emcb has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im creating a cgi script that can be called from a html page and log that hit to the site. Ill include it in a tag. What i want to do is take any variable that begins with server_,request_,http_,etc... and put them in a variable => value key/pair, but ive looked real hard and cant find out how to do it? So i sould access a variable with... $VARIABLE{server_name) Anyone know? Cheers, Elfyn

Comment on HTTP Variables In A Hash
Re: HTTP Variables In A Hash
by Kanji (Parson) on Dec 30, 2001 at 09:10 UTC

    I have a sneaking suspicion that you're looking for environmental variables, which are usually found in the %ENV hash and -- traditionally -- are in uppercase ...

    • $ENV{'SERVER_NAME'}
    • $ENV{'REQUEST_METHOD'}
    • $ENV{'HTTP_REFERER'}
    • etc.

    OTOH, if you really are after form-submitted fields, take a look at CGI.pm's (alt.) import_names() function/method, which allows you to pull everything into a seperate namespace instead of a hash ...

    import_names('VARIABLE');
    print $VARIABLE::server_name;

        --k.


Re: HTTP Variables In A Hash
by Chrisf (Friar) on Dec 30, 2001 at 14:10 UTC
    If you're just looking for the Environmental variables you can do something like so...

    my ($key, $value); while (($key, $value) = each %ENV) { print "$key = $value\n"; # or do whatever here }

Re: HTTP Variables In A Hash
by jlongino (Parson) on Dec 31, 2001 at 00:51 UTC
    The replies so far will work if you are trying to store the entire %ENV, but if you are just trying to create a subset of %ENV you can use the following (also generically for selecting hash subsets):
    use strict; use warnings; # Stuff some matches into %ENV # It's interesting to note in the output that the keys are uc'd $ENV{SERVER_junk} = 'junk'; $ENV{SERVER_test} = 'test'; $ENV{ReQuEsT_job} = 'job'; $ENV{http_header} = 'header'; my @match = qw( server_ request_ http_ ); my %hash; foreach my $key (keys %ENV) { print "$key => $ENV{$key}\n"; foreach my $test (@match) { if ($key =~ /^$test/i) { $hash{$key} = $ENV{$key}; } } } print "\nResults\n---------------------\n"; foreach (keys %hash) { print "$_ => $hash{$_}\n"; }
    Resulting Output:
    TEMP => C:\windows\TEMP SERVER_TEST => test REQUEST_JOB => job SERVER_JUNK => junk COMSPEC => C:\WINDOWS\COMMAND.COM HTTP_HEADER => header PATH => C:\PERL\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND WINDIR => C:\WINDOWS Results --------------------- SERVER_JUNK => junk SERVER_TEST => test HTTP_HEADER => header REQUEST_JOB => job

    --Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://135202]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-07-26 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (179 votes), past polls