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

Greetings, I am trying to source a cshrc file in my perl script. From the helpful reply to my previous question, I decided to use the following line: system {"/bin/csh"} "sh", "-c", "source /van/home/cshrc" || die "Cannot source the cshrc file"; This does work; however, it appears that because system spawns a new process to do this, when it finishes, I lose the environmental variables that I just sourced. Does anyone know of a way to "capture" these environmental variables so that the rest of the perl script can use them? Thanks in advance. Kirb-meister.

Replies are listed 'Best First'.
Re (tilly) 1: Sourcing cshrc environment in Perl
by tilly (Archbishop) on Mar 27, 2001 at 08:17 UTC
Re: Sourcing cshrc environment in Perl
by Dominus (Parson) on Mar 27, 2001 at 11:04 UTC
    The typical solution is something like this:
    open ENV, "csh /van/home/.cshrc; env |" or die ...; while (<ENV>) { chomp; my ($var, $val) = split /=/, $_ , 2; $ENV{$var} = $val; }
    What's the idea here? You must run a sub-process to read the .cshrc file, because to read a .cshrc file, you have to run csh. So the sub-process is mandatory. But then you get the subprocess to spit out its environment, and you load in all the appropriate settings.

    The only problem with this is that sometimes environment variables can contain newlines or other funny characters, in which case you may not be able to parse the output of the env command reliably. The easy way around this is to use your own reliable replacement for env instead of the real env. This is very easy:

    # myenv for my $var (keys %ENV) { print $var, "=", $ENV{$var}, "\0"; }
    Now the variables are terminated by \0 instead of \n, and to parse the environment data back in the main program, just use local $/ = "\0" when reading the output of myenv.

    This same trick works for /bin/sh scripts also, of course.

Re: Sourcing cshrc environment in Perl
by dws (Chancellor) on Mar 27, 2001 at 07:56 UTC
    If all you want to do is add new stuff to %ENV from an external file, then there are basically three approaches:

    1. Keep the settings as executable Perl statements in a .pl file that you can require on demand

    2. Keep the settings in some other format. Parse that format from your script, adding to %ENV as you go.

    3. exec a shell script that sources in whatever file it needs, then re-execs your Perl script (passing along some command-line option to note that the re-exec has been done once, and shouldn't proceed on indefinitely.)


    Update: Did I say three? tilly adds a very good forth below.

Re: Sourcing cshrc environment in Perl
by Anonymous Monk on Mar 27, 2001 at 07:39 UTC
    I don't know csh, but in sh and bash I can use the set command to display all environment variables (when running set in csh, it only displayed a few variables not all, don't know what's up with that but that's because, like I said, I don't know csh). A sh solution to this would be `source /path/to/file && set` then have your program parse the output. For a csh solution, all you should have to do is replace set with whatever the appropriate csh command is.