patpark has asked for the wisdom of the Perl Monks concerning the following question:

Hello all

I am little new to PERL and I am not too sure how to access Apache environment variables using PERL. I did looked into Google and many people talked about using mod_env. Unfortunately I am got no clue how I can use it.

All I am trying to do is to access / read an Apache environment variable. (Using PHP function phpinfo(), I was able to verify list of Apache environment variables)

a sample of code would be very helpful

Thank you everyone

pat
  • Comment on how to access apache environment variables

Replies are listed 'Best First'.
Re: how to access apache environment variables
by zwon (Abbot) on Feb 26, 2009 at 19:45 UTC

    The following may give you an idea how to do this:

    perl -E 'for (keys %ENV) { say "$_=$ENV{$_}"; }'
Re: how to access apache environment variables
by perrin (Chancellor) on Feb 26, 2009 at 21:01 UTC
    Although some of these variables are being set by apache, there is nothing special about them. Just access them through %ENV like you would with other environment variables.
Re: how to access apache environment variables
by leocharre (Priest) on Feb 26, 2009 at 21:03 UTC

    Yeah, it's pretty cool.

    If you're on posix , fire up a terminal and type 'set' 'env' (thank you fullermd!), this shows you the system environment variables. All of these are accessible in your perl script as : $ENV{VARNAME}.

    This is nifty, like with $ENV{HOME}- of what user your script is running as.

    The catch is .. the environment variables available to your script running called from apache will access different environment variables as called from the command line,

    So that..

    #!/usr/bin/perl use CGI ':all'; print header; print "<pre>\n"; map { print "$_ $ENV{$_}\n" } sort keys %ENV; print "</pre>\n";

    Gives you different results if called via cli or via apache (or other httpd).

    For example, cgi side, you will see 'DOCUMENT_ROOT', which is not present under the cli side. Inversely, HOME is set on cli side, but not on cgi side. Likely for security reasons.

    Different http servers will have different variables set. This depends on the configuration of the specific box in question.

      If you're on posix , fire up a terminal and type 'set'

      Not exactly...

      set is a shell function, and prints out shell variables. That may or may not include environmental variables, and may or may not include others. C shell for instance doesn't include env variables at all, only shell variables. Bourne shell does, but also includes other shell variables that aren't in the environment.

      printenv or env is probably the better choice.