1849281 Members
5524 Online
104042 Solutions
New Discussion

Re: perl question

 
Charles Li_2
Occasional Advisor

perl question

I would like to run a shell script to setup environmental variables inside a perl script.
I try to use the system command, but my understanding of the system command is for it to spawn a child process and execute the script. I need the environmental variable to be used for the perl script. Thanks.
7 REPLIES 7
John Poff
Honored Contributor

Re: perl question

Hi,

You can set environmental variables directly inside of your Perl script. Probably you should take a look at the %ENV hash, which Perl uses to store your current environment. You can do this:

$ENV{PATH} = "/bin:/usr/bin";

to set an environment variable inside of your Perl script.

JP
Rodney Hills
Honored Contributor

Re: perl question

This isn't so much a perl issue as it is a process issue. When a shell starts another shell (or perl routine), a copy of the environmental variables that are exported are made available to the new shell. Any changes to these do not effect the parent shell.

In this situation I would write create a file under /tmp that sets the variables to their new values. Then after the perl script is run and the shell script is control, use the "." command to source in the /tmp file.

example-
#!/usr/bin/sh
export x=1
perl myroutine
. /tmp/newvalues

"myroutine" would build a file called /tmp/newvalues that would assign the variables.

HTH

-- Rod Hills
There be dragons...
Steven E. Protter
Exalted Contributor

Re: perl question

Attaching a script that displays these variables.

it can be used to change them as well.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Charles Li_2
Occasional Advisor

Re: perl question

I might not have explain this clearly.
I have different scripts that sets the environmental variables depending on different situations.
When I run perl script #1 in a cron job, I would like to be able to call the set environmental script inside the perl script, so it can use the variables that I set.
Thanks.
H.Merijn Brand (procura
Honored Contributor

Re: perl question

As John already said, the ENVIRONMENT as known to *all* process types (shell, awk, sed, perl, compiled binaries, ...) is accessible in the perl hash %ENV.

Inside this hash, you can *read* the environment variable by accessing that hash with the variable's name:

my $user = $ENV{LOGNAME};

as you would have written in a shell script

export user=$LOGNAME

You can *set* a variable, by assigning a value to the hash element:

$ENV{LOGNAME} = getpwuid $<;

as you would in the shell

export LOGNAME=`logname`

Now just remember that - whatever the language you program in - an environment variable only scopes to the process itself and its children (and their children), but *never* to the calling process.

HTH, Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: perl question

Charles,

Merijn re-iterated my explaination. You can't change the variables of a parent shell from the child.

I gave an example on how you could get the results you want...

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: perl question

In my example above-
. /tmp/newvalues

Their is a space after ".". This is called sourcing a script. The script is not a child, but is run as the parent.

Hope that helps explain it...

-- Rod Hills
There be dragons...