Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid

Perl: Source shell script to for environment variables?

by Gigglesworth (Initiate)
on Feb 08, 2006 at 01:53 UTC ( #528689=perlquestion: print w/ replies, xml ) Need Help??
Gigglesworth has asked for the wisdom of the Perl Monks concerning the following question:

Hey Gang,

I'm writing a Perl script. I need it to grab some environment variables from an Oracle environment script /oracle/ . Certain environment variables such as "ORACLE_HOME" are set in that file.

If/when the DBA changes /oracle/ , I want my script to automatically use the new environment, just like any Unix shell command. That is, if the DBA removes "ORACLE_HOME" and replaces it with "ORACLE_FOO", I want my script to pick up those changes.

In a shell script, you can by running the command "source /oracle/" or ". /oracle/".

In Perl, I know how to RUN a shell command from within a Perl script via `` or qx() .

However, this doesn't change the environment of my current Perl script-- e.g. the script still doesn't know about "$ORACLE_HOME", for example.

Is there an equivilant of the 'source' command within Perl?

Is the simplist solution to write a shell script as a wrapper, like this?

#!/bin/sh . /oracle/ /path/to/perl/script/pl

Comment on Perl: Source shell script to for environment variables?
Download Code
Re: Perl: Source shell script to for environment variables?
by GrandFather (Cardinal) on Feb 08, 2006 at 02:07 UTC

    `` and qx() create a new process. Any changes that are made to the environment in a `` or qx() create process only apply for the duration of the process - they don't affect the calling Perl script.

    You can update %ENV and any process you subsequently create using `` etc will get the new environment.

    DWIM is Perl's answer to Gödel
Re: Perl: Source shell script to for environment variables?
by Tanktalus (Canon) on Feb 08, 2006 at 02:24 UTC

    Two answers. First is what I've done before (except I did it with db2profile):

    unless (exists $ENV{ORACLE_HOME}) { exec '/bin/sh', '-c', ". /oracle/; exec $^X $0 @ARGV"; }
    This reads the shell script, then reruns yourself. Hopefully this time ORACLE_HOME will be set, then you won't loop. (Although if you don't know ORACLE_HOME, how do you know which to load?)

    The second, and generally more usable, option is to mandate that the user runs the environment before running your code. Which is exactly the same as any shell or binary command - you need to set ORACLE_HOME to find Oracle and load libraries and stuff, and this is usually set, I imagine, via sourcing the environment file.

    This is actually usable because you work just like everything else. Users will have to put this in their profile or bashrc or whatever anyway, so just take advantage of it. You won't go sourcing the wrong environment if there is more than one copy installed. And you won't go using some default which may surprise the user expecting something else.

    Your shell script wrapper would work because running the shell script command (which hopefully has a different name than the perl script) implies the oracle environment that it is setting up. Although I'd make a small modification:

    if [ -z "$ORACLE_HOME" ] then . /oracle/ fi
    That way you don't go reloading a file you don't need to. Although if the user already has a different oracle environment set up, you won't use the default one which may be bad - up to you to decide.

    As for reading and parsing the shell scripts - that's not easy to do. For example, the db2profile will go and source another script (sqllib/userprofile), could do something similar. Then you have to go and follow other scripts to a very deep level. And nevermind that shell can easily call other code to do things, such as LIBPATH=`oracle_setlibpath $LIBPATH`. That can just get annoying to parse. Best to let the shell do that, and concentrate on the perl. ;-)

Re: Perl: Source shell script to for environment variables?
by graff (Chancellor) on Feb 08, 2006 at 04:35 UTC
    Is the simplist solution to write a shell script as a wrapper, like this?
    #!/bin/sh . /oracle/ /path/to/perl/script/pl

    Yes, that is the simplest way. Anything else would surely be a valuable learning experience, but relatively impractical.

Re: Perl: Source shell script to for environment variables?
by brian_d_foy (Abbot) on Feb 08, 2006 at 07:26 UTC

    The simplest solution is to run the script as part of your shell set up. Then, every process has access to it and you don't have to write a wrapper every time you want to get the Oracle variables. :)

    brian d foy <>
    Subscribe to The Perl Review

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://528689]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-04-20 11:54 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (485 votes), past polls