Are environment variables preserved through qx in a perl script

329 views Asked by At

I have some legacy perl script, which sets environment variable

$ENV{"ENV_VAR_NAME"} = $envVar;

and then uses qx() to execute another shell command

$command = "$xyz";
$result = qx($command);

Will the modified ENV_VAR_NAME be available when qx executes the new command.

1

There are 1 answers

1
Dada On BEST ANSWER

Yes.

perlvar says about %ENV:

Setting a value in ENV changes the environment for any child processes you subsequently fork() off.

And qx does indeed spawn a child process, which can therefore access your modified environment variables.

This can be easily tested:

print "1: ", qx(echo \$X); # Prints "1: "

$ENV{X} = 42;

print "2: ", qx(echo \$X); # Prints "2: 42"