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

Hi All, New to Perl so feel free to kick my ass if this is obvious.. Running Perl on XP and have written code to call command line code via `example.exe`. Works fine. Now attempting to use over web via CGI. Get an error message saying MYVAR environment variable not defined. example.exe seems ro require the MYVAR environment variable to run but can't seem to get when running CGI. Get same error if I remove MYVAR and attempt to run example.exe from command line. So, question is..how to I let example.exe 'see' windows MYVAR environment variable when running from CGI. Thanks.
  • Comment on Access to Windows Environment Variables

Replies are listed 'Best First'.
Re: Access to Windows Environment Variables
by thor (Priest) on Aug 12, 2004 at 03:54 UTC
    I'm a bit confused as to your scenario. I think you have something like the following:

    You are trying to run example.exe from a perl script that you wrote. Unfortunately, example.exe requires that the MYVAR environment variable be set. To do this, you could do something like this in your perl script:

    $ENV{MYVAR} = "MYVAR value";
    To elaborate, web servers (or at least Apache; I have little experience with others) tend to trim their environment so as to limit the amount of damage a black hat could wreak on a server where the environment was left in tact, so to speak. So, if you need a special environment variable set for something to run, you should set it explicitly as I demonstrated above.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    With all of us waiting, your kingdom will come

Re: Access to Windows Environment Variables
by neilh (Pilgrim) on Aug 12, 2004 at 03:50 UTC
    Hi, I'm not sure I understand your question here, but I'll try to answer anyway.

    To call environment variables from CGI.pm you use the %ENV hash.

    So try using $ENV{'MYVAR'} to get your information.
    A good writeup of this can be found at CGI

    PM CGI tutorials

    Neil

Re: Access to Windows Environment Variables
by ikegami (Patriarch) on Aug 12, 2004 at 04:19 UTC
    I bet the env var is probably only defined for your user and needs to be defined for all users, or at least for the user as which the web server runs. Try to right-click on My Computer, select 'Advanced', select 'Environment Variables' and enter MYVAR with the appropriate value in the System Variables area. If that doesn't work, check if you can create env vars from within your web server's configuration tool.
Re: Access to Windows Environment Variables
by PConD (Initiate) on Aug 12, 2004 at 04:28 UTC
    Huzzah!! Virtual Guinness for all

    Yep, setting up the environment variable with $ENV sorted out the problem nicely. Didn't know that apache messed around with enivonment stuff.

    Thanks all :-)

      Yup. Here's a small CGI script that I wrote - nothing special - to print out the cgi environment on Windoze XP:
      #!C:\Perl\bin use strict; ######## Start the HTML ######## print <<END_HTML; <html> <head> <title>Perl CGI Environment Variables</title> <STYLE type="text/css"> <!-- H1.page_title {text-align: center; color: blue} --> </STYLE> </head> <body> <h1 class="page_title">Perl CGI Environment Variables</h1> <table border="1"> <tr> <th bgcolor="#99ccff">\@INC Paths</th> </tr> END_HTML ### 1st print out the @INC array ### my $ct = 0; foreach my $dir (@INC) { print <<END_HTML; <tr> <td><b>$dir</b></td> </tr> END_HTML $ct++; } ######## Finish the @INC HTML ######### print <<END_HTML; </table> <br> <br> <table border="1"> <tr> <th bgcolor="#99ccff">Env Variable Name</th> <th bgcolor="#99ccff">Value</th> </tr> END_HTML ### Next print out all the environment variables ### foreach my $name (sort keys %ENV) { print <<END_HTML; <tr> <td><b>$name</b></td> <td>$ENV{$name}</td> </tr> END_HTML } ######## Finish the HTML ######### print <<END_HTML; </table> </body> </html> END_HTML
      Put this in your webserver's document root. I called it "env.pl", so access it by:
      http://your.server.com/env.pl
      and see what environment variables it displays.

      HTH.