Env::C - Get/Set/Unset Environment Variables on the C level


version 0.15


  use Env::C;
  my $key = "USER";
  $val = Env::C::getenv($key) || '';
  Env::C::setenv($key, "foobar", [$override]);
  $new_val = Env::C::getenv($key) || '';
  my $ar_env = Env::C::getallenv();
  print join "\n", @$ar_env;

      "VAR1", "value1", 1,
      "VAR2", "value2", 0

  Env::C::unsetenv_multi("VAR1", "VAR2");


This module provides a Perl API for getenv(3), setenv(3) and unsetenv(3). It also can return all the environ variables. You also can use setenv_multi and getenv_multi for bulk operations with environment.

Sometimes Perl invokes modules with underlaying C APIs which rely on certain environment variables to be set. If these variables are set in Perl and the glue code doesn't worry to set them on the C level, these variables might not be seen by the C level. This module shows what really the C level sees.



Returns the value of the environment variable matching the key or undef.

setenv($key, $value, [$override])

The setenv() function adds the variable $key to the environment with the value $value, if $key does not already exist. If $key does exist in the environment, then its value is changed to $value if $override is non-zero; if $override is zero or is not passed, then the value of $key is not changed.


The unsetenv() function deletes the variable $key from the environment.

setenv_multi($key1, $value1, $override1, $key2, $value2, $override2, ...)

Similar to setenv, but works with several variables at once.


Similar to unsetenv, but works with several variables at once.


  my $ar_env = Env::C::getallenv();
  print join "\n", @$ar_env;

The getallenv() function returns an array reference which includes all the environment variables.



Thread-safety and Thread-locality

This module should not be used in a threaded enviroment.

The OS, which maintains the struct environ, shares it between all threads in the process, which means it is not thread-local. So if you modify it in one thread, all other threads will see the new value. Something that will most likely break the code.

This module is not thread-safe, since two threads may attempt to modify/read the struct environ at the same time. I could add locking if in a threaded environment. However since the lock can't be seen by other applications, they can still bypass it causing race condition. But since thread-locality is not maintained, making this module thread-safe is useless.

If you need to modify the C level of %ENV for all threads to see, do that before threads are started. (e.g. for mod_perl 2.0, at the server startup).


  • Versions 0.01 through 0.08 written and maintained by Stas Bekman <>


The development version is on github at and may be cloned from git://


Please report any bugs or feature requests to or through the web interface at:


Michael Schout <>


This software is copyright (c) 2002 by Michael Schout.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.