in reply to Importing environment variables from shell script file to Perl

I hope I understood the question correctly, but here goes:

I'm not sure about "real" sh implementations, but bash acting as /bin/sh requires that you export your environment variables in order for subprocesses to see them.

So your myenv.sh should end with

export blah balh3
Then, in myperl-script.pl, you can access those values as $ENV{blah} and $ENV{balh3}. See perldoc perlvar for more information about the %ENV "magical" hash.

I hope this helps!


Mike

Replies are listed 'Best First'.
Re: Re: enviorment
by bugsbunny (Scribe) on Oct 23, 2003 at 12:25 UTC
    yep i was tring first this... but as i read more tha bash docs.. export-ed vars are seen only in child processes but my perl script is the parent what I mean :

    #!/usr/bin/perl
    qx{myenv.sh}
    ...more code...


    see, the env is not seen..why I need shorter solution, 'cause this will be added to many scripts and I dont want to repeat alot of similar code ..
      If the shell script that sets the variables is simple, then parsing that file is the best way to go. Substituting variable makes things a little harder but isn't too hard to accomplish in Perl. If it is more complicated than this, then it is best to let bash handle the shell scripts. Personally, I would have the config file be simple key=value format. It could be read by bash or perl, but would not be a proper shell script (no #!, no commands).

      You can use bash to run the shell script, print the environment with printenv, and parse the results with Perl

      #!bash # myenvprint.sh source myenv.sh printenv
      #!perl my $myenv = `myenvprint.sh` my %env = parse_env($myenv);