3

Win32::Env module is used for environment variable set for temporary basis. I need to set permanently, please help

Please help me to set as permanent . Because i want it use in another exe.

use Win32::Env;

SetEnv(ENV_USER, 'AUTO_EXCEL_EXPORT', 'TRUE');

By this code, i can set environment variable in windows only as temporary. when i check with echo command of that variable it is not showing. because it is set as semi permanent value.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • Does assigning to %ENV ie `$ENV{AUTO_EXCEL_EXPORT} = 'TRUE'` solve the problem? – JGNI May 30 '19 at 11:56
  • [Here](https://stackoverflow.com/questions/573817/where-are-environment-variables-stored-in-registry) is a question, that discusses where environment variables are (permanently) stored in the registry. You could add it there. – sticky bit May 30 '19 at 11:57
  • %ENV ie $ENV{AUTO_EXCEL_EXPORT} = 'TRUE' not working , it is not setting in my pc it is setting as locally for perl consolse alone. not even for this command echo %AUTO_EXCEL_EXPORT% it shows %AUTO_EXCEL_EXPORT% – Amarendhiran Amar May 30 '19 at 12:03
  • 1
    The environment is private to each process. You can spawn a new command window from inside the perl script, and it will default to a copy of the parent's process (which has the modified environment). – Raymond Chen May 30 '19 at 13:05
  • Possible duplicate of [How can an executable set an environment variable in the parent CMD or batch context?](https://stackoverflow.com/questions/46246163/how-can-an-executable-set-an-environment-variable-in-the-parent-cmd-or-batch-con) – Raymond Chen May 30 '19 at 13:07

2 Answers2

2

When assigning/changing environment variables in Windows, the Explorer subsystem needs to be notified that the changes occurred before newly opened windows/applications see the changes. If this doesn't happen, then a reboot is necessary before other processes will see the changes.

As you can see in the SYNOPSIS of Win32::Env, they've got a call to BroadcastEnv() which performs said notification task.

After reviewing the module's code, the author is setting the variables within the registry, so that call should correct the issue. Note though, that if you're running things from a cmd window while running your script, no matter what, you have to close that window and open a new one for the changes to take effect. All new windows should get the updated environment.

Example:

use Win32::Env;

SetEnv(ENV_USER, 'AUTO_EXCEL_EXPORT', 'TRUE');

BroadcastEnv();
stevieb
  • 9,065
  • 3
  • 26
  • 36
0
 use Win32::Env; 
SetEnv(ENV_USER, 'AUTO_EXCEL_EXPORT', 'FALSE');
BroadcastEnv();
print "\n";
DelEnv(ENV_USER, 'AUTO_EXCEL_EXPORT');
print "\n";
SetEnv(ENV_USER, 'AUTO_EXCEL_EXPORT', 'TRUE');
BroadcastEnv(); 

if i run this code its setting only first ENV ( false ) value, in echo command why??????