http://qs321.pair.com?node_id=645880

I thought this might come in handy for someone. This is a useful way to source a shell script to make environment variables available to your Perl script.
In our batch systems at work we primarily write our code in Perl. However, we have a number of small utilities written in shell that we use in debugging, plus we have a script that gets sourced on login that gives us access to our Database and other resources in the environment. In previous days we wrote a wrapper shell script which sourced the shell script prior to calling the Perl scripts, but this was tedious and you had to remember to use it if you ran one of the scripts from the command line, from cron, etc. Using this short bit of code, taking advantage of the '-s' option to Perl, we can eliminate the use of the wrapper script completely.
eval { exec ". ./env.sh; /usr/bin/perl -s $0 -env -- @ARGV"; } unless $env;
I've seen other solutions for this, but some rely on running the script as in a system() or open() call and then parsing the output of the 'env' command to read in and set any environment variables that were exported. If you've used other workarounds for this, I'd be interested in seeing those, too! Hope this helps someone out.

Replies are listed 'Best First'.
Re: Sourcing shell scripts
by Corion (Patriarch) on Oct 19, 2007 at 06:24 UTC

    This does not work if $0 contains whitespace ;) But that's easily remedied by either renaming your script or by adding proper quoting for whatever shell you're using.

      The only place I could think of that one might encounter spaces in $0 would be on a Windows platform. Yes, technically spaces are allowed file names in *nix but I rarely ever see it. And, the environment we're using this in is a controlled, known business environment so we're not trying to make these completely portable or cross-platform. I guess if there are some Unix folks that like putting spaces in their file/script names, then quoting could resolve the issue. Good points, though, Corion.
Re: Sourcing shell scripts
by sg (Pilgrim) on Oct 20, 2007 at 05:11 UTC

    Unless I am misunderstanding what you are doing,

    use Env;
    should give you everything you need. On Windows, I can set environment variables from inside perl via $Env{var}=<value>, and these environment variables will be visible to subsequent `backtick` calls.

      I can always set an environment variable using the built in %ENV on Unix (or you mention 'use Env' on Windows) but the idea was to make use of our existing shell environment (which is built dynamically when our software package is installed) so we can have a single source of environment settings (easier to debug, modify, etc.). We needed these shell environments set outside of the Perl script and we didn't want to duplicate them by having another Perl script to set the environment just for Perl.

      The code snippet posted will source this shell environment file prior to exec'ing the Perl script, that way it's available to the Perl code. We don't run these programs by hand, they run from a cron job, and we previously had to write a shell wrapper to perform the same function that the snippet does automatically for us. Plus, now any of our shell debugging tools and utilities are using the exact same environment settings as the actual Perl scripts that run the system. Which was the whole point.

      I hope this makes sense.

Re: Sourcing shell scripts
by noworry (Initiate) on Feb 27, 2008 at 07:30 UTC
    Hi, Thanks a lot, this post really helped in saving some rewrites !!!
Re: Sourcing shell scripts
by eldapo (Initiate) on Dec 07, 2009 at 19:54 UTC
    Thanks ^3. This post was an island of clarity in an ocean of obfuscation.
      Hi, I got this post while searching for sourcing shell script in pel. I am new to perl and quite blank in the way of executring it. Can you please explain?