how to reuse a shell script to change env from perl

Hi:

I am trying to reuse an existing shell script foo1.csh to set environment variables inside a perl script and its childern processes.

Is it possible at all to make those environment variables persistent in the main perl process and its children processes?

Do I have to create a new shell script foo2.csh to call the existing shell script foo1.csh first to set up env before launch the perl script from the new shell script foo2.csh? I am hoping there is a way to do both steps within the perl script.

Thanks!

Yes - you have to source foo1.sh inside foo2.sh. Then foo2.sh calls perl.

# foo2.sh
. foo1.sh

# invoke your perl here ---


Thanks for the quick response, Jim.

Now here is a more challenging problem:

Instead of changing the env once in the beginning, the perl script will have to load multiple sets of envs at different times.

It almost seems a deadend to me now to reuse those existing shell scripts.

Is there a way at all to do it without re-defining those env variables inside the perl all over again?

Thanks!

I do not know for sure, but I think you have design problem. It appears you ported shell script to perl. If you started with n scripts, and it was a good working design, then you should have n perl scripts as well.

If your perl is well-written, it should be using the same ENV variables names each time it runs, probably against different file sets or databases.

A standard trick in designing something like this is to use shell script "include" files.
So you will now have to extract the ENV definitions from n shell files into small ENV only definition scripts.

Then do something like this - assume we have six files names env1 env2 ... env6

#!/bin /ksh
set -A envs env1 env2 env3 env4 env5 env6
i=1
while [[ $i - lt 7 ]]
do
   . ${envs}
   # perl code call goes here
done

I do not know of a way to source a file in perl. Sourcing means that shell runs in the current process context some script. perl and shell are NOT the same language, so you would have to parse a shhell script file for variables, then call putenv for each variable.

You will have to add a subroutine to your perl code to do this, call the sub for each external file to need to "source" then run the rest of the perl code. UNless you have hundreds of external scripts, this may not be worth the effort.

[/code]

Hi Jim:

Thanks again for your help.

I am attempting to automate some of our currently manual tasks. We do not have "real" script yet, other than those shell scripts to set ENVs.

Well, I may have to extract them into perl and start from there.

Thanks!