LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-08-2007, 05:59 AM   #1
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Rep: Reputation: 15
creating environment variables in perl


hi,
can anyone help me in creating a new environment variable using perl, ??
 
Old 08-08-2007, 07:04 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Sure, can you be a bit more specific though, ie give me an example.
You can add to the built-in Perl hash %ENV. See http://perldoc.perl.org/Env.html
 
Old 08-09-2007, 02:00 AM   #3
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
thanks for u r support,
hope this will explain to u better
when i use this in my perl script,
system("FLEXLM_TIMEOUT =500000");


I am not able to create an env variable by name FLEXLM_TIMEOUT , after running the script , i checked in env , but the environment variable was not there,pls help
 
Old 08-09-2007, 03:26 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
That's because system() creates a new sub-shell, same as if you ran a shell prg.
As you (should) know, you can export env vars down a process tree, but not up.
You could tell it to print the value which would enable a a calling shell to use it.
Here, I mean print from the Perl level, not from within the system() cmd.
See http://perldoc.perl.org/functions/system.html
 
Old 08-09-2007, 07:06 AM   #5
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
ok can u tell me anyother way in perl where i can create my own environment variable , and i can access it , pls
 
Old 08-09-2007, 06:47 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Like I said, you can do it that way, but after a script has finished, the (sub)shell in which it runs is removed/destroyed by the OS.
You can save/print it to a temp file.
What exactly are you trying to achieve?
 
Old 08-10-2007, 02:35 AM   #7
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
the thing i want to achieve is , i want to create my own environment variable and store it env hash , later env variable is accessed by other scripts,

but if i do as u told ie storing the variable in a tmpfile,

then i have to use that tmpfile in every program which uses that env variable ...

ie require tmpfile;

I think thats not suitable for me , any suggestions ??

-------------------------
regards,
san
FREE as in FREEDOM thats life
 
Old 08-11-2007, 08:56 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, a prog can only affect it's own env. If you want to share values with other scripts, you can

1. Call other script from yours and pass value as cmd line arg
2. Call other script from yours and export val to new env eg system("export $var && run_new_script");
3. Use temp file
4. Use threads
5. use fork()
6. use shell and capture / fwd val on cmd line eg
#!/bin/bash
var=$(1st perl script) where script prints val to current env and is captured in $var
run_2nd_script $var
7. use a DB
8. use sockets.

As I mentioned above, progs either run in their own env and/or create a new env to output to.
There is not 1 global env shared by all you progs.
Values can be exported 'down' the process tree but not up.
It's a fundamental design thing with Unix.

HTH
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variables xeon123 Linux - General 6 05-18-2007 07:57 PM
Perl: Source shell script to for environment variables? stefanlasiewski Programming 3 02-07-2006 06:27 PM
environment variables user_linux Linux - Newbie 3 06-15-2005 08:46 AM
Environment variables and perl jrbiochem Linux - General 0 05-06-2004 09:40 AM
Environment variables Perl nbp Programming 1 10-28-2003 08:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration