Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

export environment variable from a perl program

by asifk1981 (Novice)
on Feb 02, 2005 at 15:33 UTC ( #427300=perlquestion: print w/ replies, xml ) Need Help??
asifk1981 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Can someone help me with exporting an environment variable from a perl program. This perl program is called from a shell script and I want to make use of the environment variable value from the calling shell script. I tried with $ENV{'VAR1'} = "text"; but the value of VAR1 did not get reflected in the calling shell script. Please help me....

Comment on export environment variable from a perl program
Re: export environment variable from a perl program
by trammell (Priest) on Feb 02, 2005 at 15:41 UTC
    This is a FAQ, found in Perl FAQ 8.
    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?
    
    Unix
      In the strictest sense, it can't be done--the script
      executes as a different process from the shell it was
      started from.  Changes to a process are not reflected
      in its parent--only in any children created after the
      change.  There is shell magic that may allow you to
      fake it by eval()ing the script's output in your shell;
      check out the comp.unix.questions FAQ for details.
    
Re: export environment variable from a perl program
by Tanktalus (Canon) on Feb 02, 2005 at 15:41 UTC

    Doesn't really have anything to do with perl, but with the process model most operating systems, if not all, use. That is, a child cannot affect its parent, only vice versa.

    What I've done is create a "mkAlias" perl script which sets up my environment by printing shell commands to STDOUT. I then evaluate it in shell as:

    eval `mkAliases`
    Note that the shell pretty much removes all carriage-returns in the output (not quite true, but close enough), so separate each command from perl with a semicolon:
    print qq[export VAR1="text";\n];
    Hopefully you're using a shell that can do this - if you're using CMD.EXE on Windows, sorry, but the only solution is to have perl run CMD.EXE for you.

      That is, a child cannot affect its parent, only vice versa.

      And only before the parent creates the new process (after the fork you can't twiddle the child's environment, although if you're going to exec something else you can have code that twiddles things post-fork / pre-exec to the desired state).</pedantic>

Re: export environment variable from a perl program
by cfreak (Chaplain) on Feb 02, 2005 at 17:47 UTC

    Other posters are correct, it can't be done the way you're trying. However you could make your perl program print out what you want in VAR1 and in your shell script do:

    export VAR1 = `/your/perl/script.pl`;

    Hope that helps

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://427300]
Approved by xorl
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-12-18 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls