Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i was trying to write a perl script in which i will set an environment variable like this
$ENV{"LSF_PATH"} = $SOURCE_ROOT."/tests/common/scripts";
after this i want to add more directory paths to this env variable.
in shell script we do it some ting like this
export LSF_PATH=`pwd`/$i:$LSF_PATH;where $i is path to a folder.
How this can be achieved in perl. i tried using push (@LSF_PATH,"$dirt");but it does not seem to work. how can this be made working in perl effectively
  • Comment on setting env variable while stroring prev values of that variable

Replies are listed 'Best First'.
Re: setting env variable while stroring prev values of that variable
by Corion (Patriarch) on Apr 10, 2006 at 09:20 UTC

    I think you want to do the same as in your shell script:

    use strict; use Cwd; my $pwd = getcwd; my $i = 'some_sub_directory'; my $new_path = "$pwd/$i"; $ENV{LSF_PATH} = "$new_path:$ENV{LSF_PATH}";

    If you have more than one directory you want to prepend to $ENV{LSF_PATH}, you can use the join function:

    my @new_directories = map { "$pwd/$_" } for ('some_dir', 'another_dir'); $ENV{LSF_PATH} = join ":", @new_directories, $ENV{LSF_PATH};
Re: setting env variable while stroring prev values of that variable
by swampyankee (Parson) on Apr 10, 2006 at 19:03 UTC

    Perhaps something like

    $ENV{LSF_PATH} .= $separator . $new_item;

    will work, where $separator is whatever is used to separate elements in a path (on Windows, I believe this is a semi-colon) and $new_item is the value you're adding to LSF_PATH. Pushing it into @LSF_PATH won't work without subsequently doing something like

    $ENV{LSF_PATH} = join($separator, @LSF_PATH);

    I don't think changes to the %ENV hash propogate to the environment from which Perl was called.

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele