in reply to setting perl ENV from file

Howdy!

I faced a similar question in a Korn shell environment. After trying to be tricky and parse the stinker, I realized that the simple approach was superior:

sub profile { my $self = shift; my $profile = shift; foreach (`. $profile; env`) { chomp; next unless /=/; my ($var, $value) = split(/=/, $_); $ENV{$var} = $value; } }

Pass the path to the source file to the sub and it populates %ENV... I use backtick substitution to get the shell to evaluate the profile and dump its environment in a reliable format. C shell would require some modifications to the details, but the basic structure still stands.

yours,
Michael

Replies are listed 'Best First'.
Re: setting perl ENV from file
by Abigail-II (Bishop) on Sep 17, 2003 at 01:11 UTC
    my ($var, $value) = split(/=/, $_);

    Better make that

    my ($var, $value) = split(/=/, $_, 2);

    or else it will break if a value contains a =.

    Abigail