You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by li...@free.fr on 2007/11/29 14:06:34 UTC

How to set Environment variable in a Perl script ?


Hello,

I am quite new to Apache and Mod_Perl. I have the following configuration :

    - Windows XP pro
    - Apache 2.2.4
    - ActivePerl 5.8.8
    - Mod_Perl 2.0.3

I have to port a script that runs fine under mod_cgi, to mod_perl. This script
have to run an external program, but have to set an environment variable before
launching the program. It's ok with mod_cgi, but it doesn't work with Mod_Perl.
The environment variable is not set.

I have written a small C program to test the case (LitEnv), it only display a
given environmenet variable. I also write the following script

#! d:/gcl3/programs/Perl/bin/perl
use strict;
use warnings;

my $temp = " MA VARIABLE ";
my $i = 0;
my $commande = "D:/gcl/lib/cgi-bin/LitEnv";

print "Content-type:  text/html\n\n";
print "<HTML><HEAD><TITLE>Variables du serveur</TITLE></HEAD>";
print "<BODY><CENTER>";
print "<H1> Serveur 82 : Xampp 1.6.0a - Apache 2.2.4 - ActivePerl 5.8.8 -
Mod_Perl</H1>\n";
print "<TABLE WIDTH=300><TR><TD><CENTER><FONT FACE=Arial COLOR=#000000>Table de
1000 elements dynamiques\n";
print "</FONT></CENTER></TD></TR></TABLE><BR><BR>\n";

print "#################### " . $temp . " #########################\n";
print "<FONT FACE=Arial SIZE=2>";
print "<TABLE WIDTH=750>\n";
for ($i = 0; $i < 10; ++$i)	# commence ?i = 0
				# Continue tant que $i < 10
				# Incr?nte $i avant de r?ter.
{
        $ENV{VARIABLE} = $i;

	      $temp = qx($commande);

	print "<TR>\n";
	print "    <td bgcolor=\"#D1DAE0\">Message numero </TD>\n";
	print "    <td bgcolor=\"#D1DAE0\">ENV $temp - $i </TD>\n";
	print "</TR>\n";
}#///
print "</TABLE>\n";
print "</FONT>\n";
print "<BR><BR><BR></CENTER></BODY></HTML>";


I try to setup the Perl section of the httpd.conf to 'pass' the 'VARIABLE'
environment variable but without success !

LoadFile "/gcl3/programs/perl/bin/perl58.dll"
LoadModule perl_module modules/mod_perl.so

# PerlRequire "/gcl3/programs/Apache2.2/conf/extra/startup.pl"

PerlSetupEnv on
PerlPassEnv VARIABLE

ScriptAlias /perl/ "d:/gcl/lib/cgi-bin/"
<Directory "d:/gcl/lib/cgi-bin">
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options +ExecCGI
PerlOptions +ParseHeaders
Allow from All
</Directory>

Can someone tell why it does work in mod_cgi and not with mod_perl, and what can
I do to fix it ?

Thank you for your help.

Bruno.

Re: How to set Environment variable in a Perl script ?

Posted by Perrin Harkins <pe...@elem.com>.
On Nov 29, 2007 8:06 AM,  <li...@free.fr> wrote:
> I try to setup the Perl section of the httpd.conf to 'pass' the 'VARIABLE'
> environment variable but without success !

Those settings are for passing variables into mod_perl, not for
passing them from mod_perl to an external process.  It should work to
just %ENV as you did.  Maybe this is an issue with perl threads.

- Perrin