Chris Schuld's Blog

personal musings on the composition of software

Written By: Chris Schuld Thursday, August 16th, 2007

If you ever have to set an environment variable you may run into the same reality I ran into.

A simple call to system using export (as you might do on the command line)…

system( "export MYVAR=somevalue" );

…does not work!

You have to use the Perl ENV hash variable:

$ENV{'MYVAR'} = "somevalue";

Changes to $ENV{‘SOMEVAR’} will be available to the current process and children processes only. Thus, if you change an environment variable for an upcoming system() call the process started due to the system() call will see the environment variable change.

Here is an example:

.
.
.
$ENV{'http_proxy'} = "192.168.0.10";
system( "wget --tries=2 --timeout=8 $url" );
.
.
.
Categories: Perl

Leave a Reply

About Chris

Chris Schuld

Thank you for visiting my website. I use this site for a myriad of things: maintaining software I have open sourced, connecting with my readers and friends and documenting all of this little things "I wished I would have written down." You can read more about me or contact me.


thanks for visiting... happy coding

Recent Comments