problem in setting environmental variable

R

rameshotn3

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.
 
K

kens

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
 
M

Mumia W.

[...]
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"
 
J

Jens Thoms Toerring

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
 
M

Martijn Lievaart

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top