CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Overriding the Environment Variables

 



sheet.pangasa
Novice

Dec 23, 2012, 1:53 AM

Post #1 of 6 (3121 views)
Overriding the Environment Variables Can't Post

Hi Gurus,

I am new to perl and I need some assistance here:

The operating system i am working on in Linux. We have a CLI for an application and there are two versions of the application running on the system.

For sake of simplicity we call it CLI.sh.

This CLI.sh requires an environment variable to be available in the OS which will point to some library location. We call it CLI_VAR.

The value of CLI_VAR will be different for each version of the application.

For the perl script I need to first connect to the v1 of the application and then v2. By default in the OS .profile the CLI_VAR points to v1 so connecting to v1 should not be a problem.

Now for connecting to v2 I need CLI_VAR to point to a different library. How can this be achieved. If in perl i create a variable $CLI_VAR and assign the value for v2 to it, would it work?


Code
#would this work? Will system be able to read the $CLI_VAR variable or will create a new process 

$CLI_VAR="new_library_location";
system("CLI.sh");


Your thoughts please.

Thanks!


Laurent_R
Veteran / Moderator

Dec 23, 2012, 4:11 AM

Post #2 of 6 (3117 views)
Re: [sheet.pangasa] Overriding the Environment Variables [In reply to] Can't Post

In a Perl script, the environment variables are stored in the %ENV hash.

Try for example:


Code
perl -e 'print $ENV{"HOME"};'


Your program can modify (or override locally) at least some of these environment variables, for example:


Code
$ENV{"HOME"} = "foo";


Else, you can also export environment variables un a shell script.


sheet.pangasa
Novice

Dec 23, 2012, 4:19 AM

Post #3 of 6 (3114 views)
Re: [Laurent_R] Overriding the Environment Variables [In reply to] Can't Post

Thanks for the reply.

I have a little knowledge about the ENV hash but was wondering if it would be able to override the already set env variable as the system call is going to create a child process i guess..

so for example:

at os level: CLI_VAR=abc
in script i defile $ENV{"CLI_VAR"}=def

Now if from script i call system("CLI.sh"); which CLI_VAR would it read?

Thanks.


sheet.pangasa
Novice

Dec 23, 2012, 9:59 AM

Post #4 of 6 (3102 views)
Re: [sheet.pangasa] Overriding the Environment Variables [In reply to] Can't Post

Well i think that is going to work. I do not have access to the Linux machine right now but on my windows machine i tried the below to be sure. I think it is going to work the same way irrespective of the OS:

1. Create a variable called test_var and assigned a value of abc to it.
2. Created a perl script to read the value of the test_var and it returned abcd. Change it to xyz using env hash and it worked.
3. Create a batch script which would do echo %test_var%.
4. From perl script called the batch script before and after changing the value of the environment variable and it reflected the changes, so i think i should be good now. Will let you know if i face any issues.


Code
#!/usr/bin/perl 

printf $ENV{"test_var"};

printf "\n\n\n";

printf "The output of the script is:";
system("script.bat");

$ENV{"test_var"}="xyz";

printf "\n\n\n";


printf "The output of the script is:";
system("script.bat");


Output

Code
"abcd" 


The output of the script is:
D:\Perl\env var test>echo "abcd"
"abcd"



The output of the script is:
D:\Perl\env var test>echo xyz
xyz


Thanks!


BillKSmith
Veteran

Dec 24, 2012, 7:15 AM

Post #5 of 6 (3043 views)
Re: [sheet.pangasa] Overriding the Environment Variables [In reply to] Can't Post

I agree that this shoud work on most operating systems. However, do not expect the new value to be available after your perl script ends. (Run your shell script from the command line. It will output "abcd")
Good Luck,
Bill


sheet.pangasa
Novice

Dec 26, 2012, 4:46 AM

Post #6 of 6 (2972 views)
Re: [BillKSmith] Overriding the Environment Variables [In reply to] Can't Post

Hi Bill,

Thanks for the reply.

I was able to get hold of the linux machine and tried doing the same on it. It worked and even my CLI.sh was able to read from the new variable set from inside the script instead of the one set at the OS level.

As you rightly pointed out the new value will be available only within the script itself as a new child process will be spawned once i run the script. And this is my exact requirement.

Thanks for taking time out and and providing suggestions on this.

Thanks,
Sheet.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives