problem in setting environmental variable

Discussion in 'Perl Misc' started by rameshotn3@gmail.com, Mar 5, 2007.

  1. Guest

    Hi all I searched in google for this.But no solution is working.
    I am Ramesh working on perl.I am facing one problem in exporting an
    variable.
    Requirement:
    I have to export a new variable through a perl script.


    in brief :
    -->in vi editor i am able to export like below.
    export ram=20;
    --->If I want to see the variable: echo $ram
    giving 20 as the answer.But when I put the export commnad in perl
    script & giving "echo" on vi editor it doesn't giving the value
    associated with that environmental variable

    I got the reason:
    I am giving export command in script like
    $ram=20;
    system("export ram");
    system command forking one more child process. So the value becomes
    temporary.

    How can I come out of this problem.
    Any help in this regard.
     
    , Mar 5, 2007
    #1
    1. Advertising

  2. kens Guest

    Re: problem in setting environmental variable

    On Mar 5, 4:59 am, wrote:
    > Hi all I searched in google for this.But no solution is working.
    > I am Ramesh working on perl.I am facing one problem in exporting an
    > variable.
    > Requirement:
    > I have to export a new variable through a perl script.
    >
    > in brief :
    > -->in vi editor i am able to export like below.
    > export ram=20;
    > --->If I want to see the variable: echo $ram
    > giving 20 as the answer.But when I put the export commnad in perl
    > script & giving "echo" on vi editor it doesn't giving the value
    > associated with that environmental variable
    >
    > I got the reason:
    > I am giving export command in script like
    > $ram=20;
    > system("export ram");
    > system command forking one more child process. So the value becomes
    > temporary.
    >
    > How can I come out of this problem.
    > Any help in this regard.


    If you are actually trying to set the environment variable for use
    within your program, use the %ENV hash:

    $ENV{ram} = 20;

    Assuming 'ram' is the name of the environment variable.

    HTH, Ken
     
    kens, Mar 5, 2007
    #2
    1. Advertising

  3. Mumia W. Guest

    On 03/05/2007 03:59 AM, wrote:
    > [...]
    > I am giving export command in script like
    > $ram=20;
    > system("export ram");


    The variable is exported automatically when you set a key in %ENV.

    $ENV{ram} = 20;
    system ('echo $ram');


    > system command forking one more child process. So the value becomes
    > temporary.
    >


    It will always be temporary.

    > How can I come out of this problem.
    > Any help in this regard.
    >


    There is no way to do this directly. Read "perldoc -q environment"
     
    Mumia W., Mar 5, 2007
    #3
  4. wrote:
    > Hi all I searched in google for this.But no solution is working.
    > I am Ramesh working on perl.I am facing one problem in exporting an
    > variable.
    > Requirement:
    > I have to export a new variable through a perl script.


    > in brief :
    > -->in vi editor i am able to export like below.
    > export ram=20;
    > --->If I want to see the variable: echo $ram
    > giving 20 as the answer.But when I put the export commnad in perl
    > script & giving "echo" on vi editor it doesn't giving the value
    > associated with that environmental variable


    You can't set environment variables for other processes from within
    a program. Your programs environment is inherited from the process
    that started it (perhaps the shell). Once the program is started its
    environment is its private property, nothing than the program itself
    can change it. So everything you can change from within a Perl script
    is the environment of the script itself and, of course, the environ-
    ments of the processes started afterwards from within the script
    (since they inherit its environment). But it is simply not possible
    to change the environment of any other program that is already run-
    ning.

    > I got the reason:
    > I am giving export command in script like
    > $ram=20;
    > system("export ram");


    That should probably be 'system("export $ram");'

    > system command forking one more child process. So the value becomes
    > temporary.


    system() creates a shell, which then has that environment variable
    being set. But that doesn't help you a bit, since it then quits
    immediately.

    > How can I come out of this problem.


    You can't, sorry. The environment isn't what you seem to think it
    is. It isn't a medium to exchange informations between processes.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 5, 2007
    #4
  5. On Mon, 05 Mar 2007 13:30:57 +0000, Jens Thoms Toerring wrote:

    >> How can I come out of this problem.

    >
    > You can't, sorry. The environment isn't what you seem to think it
    > is. It isn't a medium to exchange informations between processes.


    Actually it is. But only from parent to child, which is where the OP goes
    wrong in his thinking.

    M4
     
    Martijn Lievaart, Mar 5, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kourosh
    Replies:
    2
    Views:
    918
    Joe Kesselman
    May 20, 2006
  2. iavian
    Replies:
    7
    Views:
    1,334
    Keith Thompson
    May 11, 2008
  3. jackster the jackle

    Setting Environmental Variables in Ruby script

    jackster the jackle, Oct 15, 2008, in forum: Ruby
    Replies:
    3
    Views:
    95
    jackster the jackle
    Oct 15, 2008
  4. timr
    Replies:
    2
    Views:
    119
    Robert Klemme
    Jan 20, 2009
  5. John Smith

    Environmental variable for CGI proxy

    John Smith, May 16, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    161
    Gunnar Hjalmarsson
    May 16, 2006
Loading...

Share This Page