Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > problem in setting environmental variable

Reply
Thread Tools

problem in setting environmental variable

 
 
rameshotn3@gmail.com
Guest
Posts: n/a
 
      03-05-2007
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.

 
Reply With Quote
 
 
 
 
kens
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 4:59 am, ramesho...@gmail.com 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

 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      03-05-2007
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"


 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible to override a 'read only' environmental variable like$" ? timr Ruby 2 01-20-2009 10:47 AM
Setting Environmental Variables in Ruby script jackster the jackle Ruby 3 10-15-2008 05:53 PM
How do I export a environmental variable to a C program? iavian C Programming 7 05-11-2008 03:25 AM
possible to use an environmental variable to address the XSL file path? Kourosh XML 2 05-20-2006 03:35 AM
Environmental variable for CGI proxy John Smith Perl Misc 1 05-16-2006 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57