#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Exclamation Environment Variable Setting Using Perl


    Hi All,

    I need to set the environment variable in perl script. but I am not able to do that. Since I need to set at the parent process.

    I Can also set the environment variable in /etc/profile but the issue is same variable value has to be changed 2 times in perl script so I am not able to do that.

    Please can u guide me to change using perl script without shell script.

    Regard's
    Ravi R N
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    I need to set the environment variable in perl script. but I am not able to do that. Since I need to set at the parent process.
    Perl scripts can't alter the environment of their parent process. This limitation is not limited to just Perl. PHP, Python, etc, have the same limitation.

    You can change the environment of the Perl script and it child processes by altering the %ENV hash.

    If you need to alter the env of the parent, that needs to be done in the shell prior to executing the Perl script.

    I Can also set the environment variable in /etc/profile but the issue is same variable value has to be changed 2 times in perl script so I am not able to do that.
    If you need to change the ENV twice within the Perl script, then that tells me that the ENV of your Perl script and its children need to be altered, not the parent.

    Please explain what you really need to accomplish AND post your Perl script so that we can help you accomplish your goals.

    FYI, you have an XY problem. Meaning that you have problem X and what to use your faulty Y solution and are asking us how to implement your faulty solution rather than asking us how to solve the base problem.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by FishMonger
    Perl scripts can't alter the environment of their parent process. This limitation is not limited to just Perl. PHP, Python, etc, have the same limitation.

    You can change the environment of the Perl script and it child processes by altering the %ENV hash.

    If you need to alter the env of the parent, that needs to be done in the shell prior to executing the Perl script.


    If you need to change the ENV twice within the Perl script, then that tells me that the ENV of your Perl script and its children need to be altered, not the parent.

    Please explain what you really need to accomplish AND post your Perl script so that we can help you accomplish your goals.

    FYI, you have an XY problem. Meaning that you have problem X and what to use your faulty Y solution and are asking us how to implement your faulty solution rather than asking us how to solve the base problem.

    Hi,

    My perl script is shown below.


    offshore@jania-suse10:~/v6> cat test.pl
    #!/usr/bin/perl

    system ("export LD_LIBRARY_PATH=/opt/lib");

    When I execute the above perl script it is working fine but when i see on the shell it is not showing as shown below.

    offshore@jania-suse10:~/Ravi/it4/v6> echo $LD_LIBRARY_PATH


    my machine details are shown below:

    Linux jania-suse10 2.6.16.21-0.8-smp

    Could you please help me to set the path using perl script.

    Regards
    Ravi R N
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Ok, you do not understand how environments work in *nix (and this is irrespective of whatever your programming language is). When you set an environment variable, any other programs that your script starts will inherit a copy of your environment (and therefore see the variable). As each program inherits a copy of the parent's environment, the parent will not see the child's changes.

    Therefore, if you do this:
    Code:
    #!/usr/bin/perl
    
    system ("export LD_LIBRARY_PATH=/opt/lib");
    When you run it, the shell calls perl and passes it its copy of the environment. The perl program in turn calls another shell program via system() and passes it a copy of perl's environment. The third program (the new shell) alters its copy of the environment and then returns to perl. At this point, perl's copy of the environment is unaltered. And when perl returns back to the first shell that spawned it, that shell's copy of the environment is unaltered too.

    Now if you did this:
    Code:
    #!/usr/bin/perl
    
    $ENV{LD_LIBRARY_PATH} = "/opt/lib";
    system ("./my_program");
    Now when you run this, the shell program will pass a copy of its environment to the perl program, the perl program runs and alters its own environment and then calls another shell program (my_program) via the system() command. my_program gets a copy of perl's environment (with the LD_LIBRARY_PATH set) and can see the altered LD_LIBRARY_PATH. A program can alter its own environment and the children it starts up will see the altered environment. However, you cannot set the environment of the parent.

    More to the point, what the heck are you trying to do with the perl script and why can you not set the environment from .bashrc or your crontab file, as the case may be.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Exclamation


    Hi,

    My perl script is shown below.


    offshore@jania-suse10:~/v6> cat test.pl
    #!/usr/bin/perl

    system ("export LD_LIBRARY_PATH=/opt/lib");

    When I execute the above perl script it is working fine but when i see on the shell it is not showing as shown below.

    offshore@jania-suse10:~/Ravi/it4/v6> echo $LD_LIBRARY_PATH


    my machine details are shown below:

    Linux jania-suse10 2.6.16.21-0.8-smp

    Could you please help me to set the path using perl script.

    Regards
    Ravi R N
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    http://www.mikeash.com/getting_answers.html
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    @ravirn: Give it up dude. You obviously have no clue what is going on. Even after two explanations, you still keep repeating the same question. Best thing to do is to return your customer's money back to him/her and tell him/her that it is beyond your skill set. At least he'll appreciate your honesty and recommend you to others.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    @Scorpions4ever : Thanks for your valuable time in replying I got the solution I have implemented also. First of all I would like to say that please do not underestimate any ones skill set. Please be cautious before making the words.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    We can only gauge your skill set by basing it on your questions and responses to our answers. So far, your questions and responses indicate to us that in this matter, you have a very low skill set.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally Posted by ravirn
    @Scorpions4ever : Thanks for your valuable time in replying I got the solution I have implemented also. First of all I would like to say that please do not underestimate any ones skill set. Please be cautious before making the words.
    I call. What is your solution?? Please post it here.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo