Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

How to set an environment variable in perl so that it's visible to subsequent processes?

by Zombie anzalone (Initiate)
on Sep 17, 2002 at 01:18 UTC ( #198414=categorized question: print w/replies, xml ) Need Help??
Contributed by Zombie anzalone on Sep 17, 2002 at 01:18 UTC
Q&A  > programs and processes


I want to set an environment variable from within the perl script so that it's visible to other subsequent processes, like what export var=xyz does in unix shells. Something like $ENV{'var'} = 'xyz' has effect only for the current process and any children it spawns.

Answer: How to set an environment variable in perl so that it's visible to subsequent processes?
contributed by jdporter

For Unix at least, the short answer is that it can't be done. This is in the Perl FAQ, by the way: I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

Answer: How to set an environment variable in perl so that it's visible to subsequent processes?
contributed by Zombie anzalone

On Windows, you can use the Win32::AdminMisc module (by Dave Roth, not on CPAN), which has a function for this, SetEnvVar:

SetEnvVar( $Name, $Value [, $Type [, $Timeout ] ] )

This will set an environment variable $Name to $Value. This function will set the variable (creating it if $Name does not exist).
The new value will be set globally so all applications will be able to use it, unlike setting $ENV{xxx} — which sets the variable for the current process and any children it spawns afterwards.
When this function is called a broadcast to all running applications is made telling them about the variable change. An application may choose to ignore the change if it was programmed to do so.
If $Timeout is specified then the broadcast announcement will be aborted if it takes longer than $Timeout seconds (some applications may be in a hung state so they can not acknowledge the broadcast). If this timeout occurs the variable will still be updated but broadcasting it's change will be aborted.
If $Type is specified it can be one of:

Specifies that $Name will be a system environment variable. (default if $Type is not specified)
Specifies that $Name will be a user environment variable.

This function is the equivalent to setting an environment variable in the control panel's system applet.

NOTE: Your script will not see the new value. For that you should set $ENV{xxx}.


Win32::AdminMisc::SetEnvVar( "Temp", "$ENV{HOMEDRIVE}$ENV{HOMEPATH}", +ENV_USER, 10 );


0 if not successful
1 if successful

Win32::AdminMisc also has a DelEnvVar function for deleting environment variables.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [karlgoethebier]: Discipulus: Mmh, who disappeared?
    [marto]: morning all
    [shmem]: perl -lE '$_="odd mornin";($o=$_)=~ s/^|$/g/g;say$o'
    [Eily]: Hi monks o/
    [choroba]: 3 PM in Vietnam, BTW
    Discipulus maybe a wrong impression..
    [karlgoethebier]: Discipulus: Workshops are for weenies ;-)
    [Discipulus]: here 43C high umidity, tiger mosquitos, violence in the street.. everywhere is vietnam
    [karlgoethebier]: OK, i'm socially unacceptable
    [karlgoethebier]: Discipulus: Were the hell are you today?

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2017-06-27 08:08 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (600 votes). Check out past polls.