Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

Replies are listed 'Best First'.
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?
      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/`;

    Hope that helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://427300]
Approved by xorl
[Lady_Aleena]: So putting a nice DE or WM up on a Linux machine is not an option?
[Discipulus]: i need some converter to have directly an OVF format directly: at the moment i'm trying with vmware converter standalone
[Lady_Aleena]: I don't know what to tell you. I can barely get my Linux machine to do things I want.
[Discipulus]: if DE or WM means desktop env and win manager.. no, not an option
[Lady_Aleena]: That's what they mean, at least to me. 8)
[Discipulus]: in the first attempt i failed, with an not usable v-machine.. ;=)
[Discipulus]: but then discovered a registry trick to do before the physical2virtual migration
[Lady_Aleena]: Sounds like a headache.
[Discipulus]: a bit shaggy.. every atttempt is ~4hours to virtualize and 1-2hours to kick the big file to boot..

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-05-22 18:45 GMT
Find Nodes?
    Voting Booth?