title: How do I set an Win32 system environment variable in Perl?
body: <p>I want to be able to set a system environment variable in Perl and I wrote the following script (inspired from <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2005/03/18/how-can-i-create-an-environment-variable-using-a-script.aspx" rel="nofollow">this idea</a>) to accomplish this:</p>
  
  <pre><code>use Win32;
  use Win32::API;
  use Win32::OLE qw( in );
  
  my $machine = ".";
  my $WMIServices = Win32::OLE-&gt;GetObject ( "winmgmts:{impersonationLevel=impersonate,(security)}//$machine/root/cimv2" ) || die;
  
  my $objVariable = $WMIServices-&gt;Get("Win32_Environment")-&gt;SpawnInstance_;
  $objVariable-&gt;{Name}="NewOSType";
  $objVariable-&gt;{UserName} = "&lt;System&gt;";
  $objVariable-&gt;{VariableValue} = "LAMBDA";
  $objVariable-&gt;Put_;
  </code></pre>
  
  <p>However, I am not an expert in Perl and I would like to know what the experts think about this code. Is this the best way to accomplish this?</p>